xtd - Reference Guide 0.2.0
file.h
Go to the documentation of this file.
1 #pragma once
5 
6 #include <chrono>
7 #include <cstdint>
8 #include <cstdio>
9 #include <fstream>
10 #include <vector>
11 #include "../core_export.h"
12 #include "../date_time.h"
13 #include "../static.h"
14 #include "../ustring.h"
15 #include "file_attributes.h"
16 #include "file_permissions.h"
17 #include "stream_reader.h"
18 #include "stream_writer.h"
19 
21 namespace xtd {
23  namespace io {
35  class core_export_ file static_ {
36  public:
38 
47  template<typename type_t>
48  static void append_all_lines(const xtd::ustring& path, type_t contents) {
49  xtd::io::stream_writer sw(path, true);
50  for (const auto& line : contents)
51  sw.write_line(line);
52  }
53 
61  template<typename type_t>
62  static void append_all_lines(const xtd::ustring& path, const std::initializer_list<type_t>& contents) {
63  xtd::io::stream_writer sw(path, true);
64  for (const auto& line : contents)
65  sw.write_line(line);
66  }
67 
74  template<typename type_t>
75  static void append_all_text(const xtd::ustring& path, type_t text) {
76  xtd::io::stream_writer sw(path, true);
77  sw.write(text);
78  }
79 
85  static std::ofstream append_text(const xtd::ustring& path);
86 
93  static void copy(const xtd::ustring& src, const xtd::ustring& dest);
94 
101  static void copy(const xtd::ustring& src, const xtd::ustring& dest, bool overwrite);
102 
108  static std::ofstream create(const xtd::ustring& path);
109 
116  static std::ofstream create_text(const xtd::ustring& path);
117 
123  static bool exists(const xtd::ustring& path) noexcept;
124 
129  static xtd::io::file_attributes get_attributes(const xtd::ustring& path);
130 
138  static xtd::date_time get_creation_time(const xtd::ustring& path);
139 
147  static xtd::date_time get_last_access_time(const xtd::ustring& path);
148 
156  static xtd::date_time get_last_write_time(const xtd::ustring& path);
157 
162  static xtd::io::file_permissions get_permissions(const xtd::ustring& path);
163 
171  static void move(const xtd::ustring& src, const xtd::ustring& dest);
172 
181  static void move(const xtd::ustring& src, const xtd::ustring& dest, bool overwrite);
182 
189  static std::fstream open(const xtd::ustring& path, std::ios::openmode mode);
190 
196  static std::ifstream open_read(const xtd::ustring& path);
197 
203  static std::ifstream open_text(const xtd::ustring& path);
204 
210  static std::ofstream open_write(const xtd::ustring& path);
211 
218  static std::vector<uint8_t> read_all_bytes(const xtd::ustring& path);
219 
226  template<typename char_t>
227  static std::vector<uint8_t> read_all_bytes(const char_t* path) {return read_all_bytes(xtd::ustring(path));}
228 
234  static std::vector<xtd::ustring> read_all_lines(const xtd::ustring& path);
235 
241  static xtd::ustring read_all_text(const xtd::ustring& path);
242 
247  static void remove(const xtd::ustring& path);
248 
255  static void replace(const xtd::ustring& source_file_name, const xtd::ustring& destination_file_name, const xtd::ustring& destination_backup_file_name);
256 
269  static void set_attributes(const xtd::ustring& path, xtd::io::file_attributes attributes);
270 
282  static void set_creation_time(const xtd::ustring& path, const xtd::date_time& creation_time);
283 
294  static void set_last_access_time(const xtd::ustring& path, const xtd::date_time& last_access_time);
295 
306  static void set_last_write_time(const xtd::ustring& path, const xtd::date_time& last_write_time);
307 
318  static void set_permissions(const xtd::ustring& path, xtd::io::file_permissions permissions);
319 
327  template<typename type_t>
328  static void write_all_lines(const xtd::ustring& path, type_t contents) {
329  xtd::io::stream_writer sw(path);
330  for (const auto& line : contents)
331  sw.write_line(line);
332  }
333 
341  template<typename type_t>
342  static void write_all_lines(const xtd::ustring& path, const std::initializer_list<type_t>& contents) {
343  xtd::io::stream_writer sw(path);
344  for (const auto& line : contents)
345  sw.write_line(line);
346  }
347 
354  template<typename type_t>
355  static void write_all_text(const xtd::ustring& path, type_t text) {
356  xtd::io::stream_writer sw(path);
357  sw.write(text);
358  }
359 
365  static std::ofstream write_text(const xtd::ustring& path);
367  };
368  }
369 }
static std::vector< uint8_t > read_all_bytes(const char_t *path)
Opens a binary file, reads the contents of the file into a byte array, and then closes the file...
Definition: file.h:227
Contains xtd::io::stream_writer class.
#define static_
This keyword is use to represent a static object. A static object can&#39;t be instantiated (constructors...
Definition: static.h:38
file_attributes
Provides attributes for files and directories.
Definition: file_attributes.h:20
static void write_all_lines(const xtd::ustring &path, const std::initializer_list< type_t > &contents)
Writes lines to a file, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.
Definition: file.h:342
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
Provides static methods for the creation, copying, deletion, moving, and opening of files...
Definition: file.h:35
Contains xtd::io::file_permissions enum class.
void write(const xtd::ustring &value) override
Writes the specified string value to the text stream.
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:50
static void append_all_text(const xtd::ustring &path, type_t text)
Appends text to a file, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.
Definition: file.h:75
Performs operations on std::basic_string instances that contain file or directory path information...
Definition: path.h:32
static void write_all_lines(const xtd::ustring &path, type_t contents)
Writes lines to a file, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.
Definition: file.h:328
Represents an instant in time, typically expressed as a date and time of day.
Definition: date_time.h:71
Implements a xtd::io::text_writer for writing characters to a stream.
Definition: stream_writer.h:25
Contains xtd::io::file_attributes enum class.
void write_line()
Writes new line to the text stream.
static void write_all_text(const xtd::ustring &path, type_t text)
Writes text to a file, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.
Definition: file.h:355
static void append_all_lines(const xtd::ustring &path, type_t contents)
Appends lines to a file, and then closes the file. If the specified file does not exist...
Definition: file.h:48
static void append_all_lines(const xtd::ustring &path, const std::initializer_list< type_t > &contents)
Appends lines to a file, and then closes the file. If the specified file does not exist...
Definition: file.h:62
Contains xtd::io::stream_reader class.
file_permissions
Provides permissions for files and directories.
Definition: file_permissions.h:20