xtd 0.2.0
file.h
Go to the documentation of this file.
1 #pragma once
5 #include "file_attributes.h"
6 #include "file_permissions.h"
7 #include "stream_reader.h"
8 #include "stream_writer.h"
9 #include "../chrono.h"
10 #include "../core_export.h"
11 #include "../date_time.h"
12 #include "../static.h"
13 #include "../ustring.h"
14 #include <cstdint>
15 #include <cstdio>
16 #include <fstream>
17 #include <tuple>
18 #include <vector>
19 
21 namespace xtd {
23  namespace io {
37  class core_export_ file static_ {
38  public:
40 
49  template<typename type_t>
50  static void append_all_lines(const xtd::ustring& path, type_t contents) {
51  xtd::io::stream_writer sw(path, true);
52  for (const auto& line : contents)
53  sw.write_line(line);
54  }
55 
63  template<typename type_t>
64  static void append_all_lines(const xtd::ustring& path, const std::initializer_list<type_t>& contents) {
65  xtd::io::stream_writer sw(path, true);
66  for (const auto& line : contents)
67  sw.write_line(line);
68  }
69 
76  template<typename type_t>
77  static void append_all_text(const xtd::ustring& path, type_t text) {
78  xtd::io::stream_writer sw(path, true);
79  sw.write(text);
80  }
81 
87  static std::ofstream append_text(const xtd::ustring& path);
88 
95  static void copy(const xtd::ustring& src, const xtd::ustring& dest);
96 
103  static void copy(const xtd::ustring& src, const xtd::ustring& dest, bool overwrite);
104 
110  static std::ofstream create(const xtd::ustring& path);
111 
118  static std::ofstream create_text(const xtd::ustring& path);
119 
125  static bool exists(const xtd::ustring& path) noexcept;
126 
131  static xtd::io::file_attributes get_attributes(const xtd::ustring& path);
132 
140  static xtd::date_time get_creation_time(const xtd::ustring& path);
141 
149  static xtd::date_time get_last_access_time(const xtd::ustring& path);
150 
158  static xtd::date_time get_last_write_time(const xtd::ustring& path);
159 
164  static xtd::io::file_permissions get_permissions(const xtd::ustring& path);
165 
173  static void move(const xtd::ustring& src, const xtd::ustring& dest);
174 
183  static void move(const xtd::ustring& src, const xtd::ustring& dest, bool overwrite);
184 
191  static std::fstream open(const xtd::ustring& path, std::ios::openmode mode);
192 
198  static std::ifstream open_read(const xtd::ustring& path);
199 
205  static std::ifstream open_text(const xtd::ustring& path);
206 
212  static std::ofstream open_write(const xtd::ustring& path);
213 
220  static std::vector<xtd::byte> read_all_bytes(const xtd::ustring& path);
221 
228  template<typename char_t>
229  static std::vector<xtd::byte> read_all_bytes(const char_t* path) {return read_all_bytes(xtd::ustring(path));}
230 
236  static std::vector<xtd::ustring> read_all_lines(const xtd::ustring& path);
237 
243  static xtd::ustring read_all_text(const xtd::ustring& path);
244 
249  static void remove(const xtd::ustring& path);
250 
257  static void replace(const xtd::ustring& source_file_name, const xtd::ustring& destination_file_name, const xtd::ustring& destination_backup_file_name);
258 
271  static void set_attributes(const xtd::ustring& path, xtd::io::file_attributes attributes);
272 
284  static void set_creation_time(const xtd::ustring& path, const xtd::date_time& creation_time);
285 
296  static void set_last_access_time(const xtd::ustring& path, const xtd::date_time& last_access_time);
297 
308  static void set_last_write_time(const xtd::ustring& path, const xtd::date_time& last_write_time);
309 
320  static void set_permissions(const xtd::ustring& path, xtd::io::file_permissions permissions);
321 
329  template<typename type_t>
330  static void write_all_lines(const xtd::ustring& path, type_t contents) {
331  xtd::io::stream_writer sw(path);
332  for (const auto& line : contents)
333  sw.write_line(line);
334  }
335 
343  template<typename type_t>
344  static void write_all_lines(const xtd::ustring& path, const std::initializer_list<type_t>& contents) {
345  xtd::io::stream_writer sw(path);
346  for (const auto& line : contents)
347  sw.write_line(line);
348  }
349 
356  template<typename type_t>
357  static void write_all_text(const xtd::ustring& path, type_t text) {
358  xtd::io::stream_writer sw(path);
359  sw.write(text);
360  }
361 
367  static std::ofstream write_text(const xtd::ustring& path);
369  private:
370  static std::tuple<time_t, time_t, time_t> get_file_times(const ustring& path);
371  };
372  }
373 }
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:37
file_attributes
Provides attributes for files and directories.
Definition: file_attributes.h:22
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:344
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:37
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:46
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:77
Performs operations on std::basic_string instances that contain file or directory path information...
Definition: path.h:34
static std::vector< xtd::byte > 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:229
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:330
Represents an instant in time, typically expressed as a date and time of day.
Definition: date_time.h:78
Implements a xtd::io::text_writer for writing characters to a stream.
Definition: stream_writer.h:26
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:357
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:50
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:64
Contains xtd::io::stream_reader class.
file_permissions
Provides permissions for files and directories.
Definition: file_permissions.h:22