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 <cstdint>
7 #include <cstdio>
8 #include <fstream>
9 #include <vector>
10 #include "../core_export.h"
11 #include "../static.h"
12 #include "../ustring.h"
13 #include "file_attributes.h"
14 #include "stream_reader.h"
15 #include "stream_writer.h"
16 
18 namespace xtd {
20  namespace io {
27  class core_export_ file static_ {
28  public:
36  template<typename type_t>
37  static void append_all_lines(const xtd::ustring& path, type_t contents) {
38  xtd::io::stream_writer sw(path, true);
39  for (const auto& line : contents)
40  sw.write_line(line);
41  }
42 
50  template<typename type_t>
51  static void append_all_lines(const xtd::ustring& path, const std::initializer_list<type_t>& contents) {
52  xtd::io::stream_writer sw(path, true);
53  for (const auto& line : contents)
54  sw.write_line(line);
55  }
56 
63  template<typename type_t>
64  static void append_all_text(const xtd::ustring& path, type_t text) {
65  xtd::io::stream_writer sw(path, true);
66  sw.write(text);
67  }
68 
74  static std::ofstream append_text(const xtd::ustring& path);
75 
82  static void copy(const xtd::ustring& src, const xtd::ustring& dest);
83 
90  static void copy(const xtd::ustring& src, const xtd::ustring& dest, bool overwrite);
91 
97  static std::ofstream create(const xtd::ustring& path);
98 
105  static std::ofstream create_text(const xtd::ustring& path);
106 
112  static bool exists(const xtd::ustring& path) noexcept;
113 
118  static xtd::io::file_attributes get_attributes(const xtd::ustring& src);
119 
127  static void move(const xtd::ustring& src, const xtd::ustring& dest);
128 
135  static std::fstream open(const xtd::ustring& path, std::ios::openmode mode);
136 
142  static std::ifstream open_read(const xtd::ustring& path);
143 
149  static std::ifstream open_text(const xtd::ustring& path);
150 
156  static std::ofstream open_write(const xtd::ustring& path);
157 
164  static std::vector<uint8_t> read_all_bytes(const xtd::ustring& path);
165 
172  template<typename char_t>
173  static std::vector<uint8_t> read_all_bytes(const char_t* path) {return read_all_bytes(xtd::ustring(path));}
174 
180  static std::vector<xtd::ustring> read_all_lines(const xtd::ustring& path);
181 
187  static xtd::ustring read_all_text(const xtd::ustring& path);
188 
193  static void remove(const xtd::ustring& path);
194 
201  static void replace(const xtd::ustring& source_file_name, const xtd::ustring& destination_file_name, const xtd::ustring& destination_backup_file_name);
202 
210  template<typename type_t>
211  static void write_all_lines(const xtd::ustring& path, type_t contents) {
212  xtd::io::stream_writer sw(path);
213  for (const auto& line : contents)
214  sw.write_line(line);
215  }
216 
224  template<typename type_t>
225  static void write_all_lines(const xtd::ustring& path, const std::initializer_list<type_t>& contents) {
226  xtd::io::stream_writer sw(path);
227  for (const auto& line : contents)
228  sw.write_line(line);
229  }
230 
237  template<typename type_t>
238  static void write_all_text(const xtd::ustring& path, type_t text) {
239  xtd::io::stream_writer sw(path);
240  sw.write(text);
241  }
242 
248  static std::ofstream write_text(const xtd::ustring& path);
249  };
250  }
251 }
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:173
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:17
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:225
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:27
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:48
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:64
Performs operations on std::basic_string instances that contain file or directory path information...
Definition: path.h:29
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:211
Implements a xtd::io::text_writer for writing characters to a stream.
Definition: stream_writer.h:20
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:238
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:37
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:51
Contains xtd::io::stream_reader class.