xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
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 "stream_reader.h"
17 #include "stream_writer.h"
18 
20 namespace xtd {
22  namespace io {
34  class core_export_ file static_ {
35  public:
37 
46  template<typename type_t>
47  static void append_all_lines(const xtd::ustring& path, type_t contents) {
48  xtd::io::stream_writer sw(path, true);
49  for (const auto& line : contents)
50  sw.write_line(line);
51  }
52 
60  template<typename type_t>
61  static void append_all_lines(const xtd::ustring& path, const std::initializer_list<type_t>& contents) {
62  xtd::io::stream_writer sw(path, true);
63  for (const auto& line : contents)
64  sw.write_line(line);
65  }
66 
73  template<typename type_t>
74  static void append_all_text(const xtd::ustring& path, type_t text) {
75  xtd::io::stream_writer sw(path, true);
76  sw.write(text);
77  }
78 
84  static std::ofstream append_text(const xtd::ustring& path);
85 
92  static void copy(const xtd::ustring& src, const xtd::ustring& dest);
93 
100  static void copy(const xtd::ustring& src, const xtd::ustring& dest, bool overwrite);
101 
107  static std::ofstream create(const xtd::ustring& path);
108 
115  static std::ofstream create_text(const xtd::ustring& path);
116 
122  static bool exists(const xtd::ustring& path) noexcept;
123 
128  static xtd::io::file_attributes get_attributes(const xtd::ustring& path);
129 
137  static xtd::date_time get_creation_time(const xtd::ustring& path);
138 
146  static xtd::date_time get_last_access_time(const xtd::ustring& path);
147 
155  static xtd::date_time get_last_write_time(const xtd::ustring& path);
156 
164  static void move(const xtd::ustring& src, const xtd::ustring& dest);
165 
174  static void move(const xtd::ustring& src, const xtd::ustring& dest, bool overwrite);
175 
182  static std::fstream open(const xtd::ustring& path, std::ios::openmode mode);
183 
189  static std::ifstream open_read(const xtd::ustring& path);
190 
196  static std::ifstream open_text(const xtd::ustring& path);
197 
203  static std::ofstream open_write(const xtd::ustring& path);
204 
211  static std::vector<uint8_t> read_all_bytes(const xtd::ustring& path);
212 
219  template<typename char_t>
220  static std::vector<uint8_t> read_all_bytes(const char_t* path) {return read_all_bytes(xtd::ustring(path));}
221 
227  static std::vector<xtd::ustring> read_all_lines(const xtd::ustring& path);
228 
234  static xtd::ustring read_all_text(const xtd::ustring& path);
235 
240  static void remove(const xtd::ustring& path);
241 
248  static void replace(const xtd::ustring& source_file_name, const xtd::ustring& destination_file_name, const xtd::ustring& destination_backup_file_name);
249 
257  template<typename type_t>
258  static void write_all_lines(const xtd::ustring& path, type_t contents) {
259  xtd::io::stream_writer sw(path);
260  for (const auto& line : contents)
261  sw.write_line(line);
262  }
263 
271  template<typename type_t>
272  static void write_all_lines(const xtd::ustring& path, const std::initializer_list<type_t>& contents) {
273  xtd::io::stream_writer sw(path);
274  for (const auto& line : contents)
275  sw.write_line(line);
276  }
277 
284  template<typename type_t>
285  static void write_all_text(const xtd::ustring& path, type_t text) {
286  xtd::io::stream_writer sw(path);
287  sw.write(text);
288  }
289 
295  static std::ofstream write_text(const xtd::ustring& path);
297  };
298  }
299 }
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:220
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:272
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:34
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:74
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:258
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:285
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:47
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:61
Contains xtd::io::stream_reader class.