xtd 0.2.0
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1
4#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
21namespace xtd {
23 namespace io {
38 public:
40
49 template<typename type_t>
50 static void append_all_lines(const xtd::ustring& path, type_t contents) {
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) {
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) {
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
132
141
150
159
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
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
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
321
329 template<typename type_t>
330 static void write_all_lines(const xtd::ustring& path, type_t contents) {
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) {
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) {
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}
Represents an instant in time, typically expressed as a date and time of day.
Definition date_time.h:79
Provides static methods for the creation, copying, deletion, moving, and opening of files,...
Definition file.h:37
static std::ifstream open_read(const xtd::ustring &path)
Opens an existing file for reading.
static void move(const xtd::ustring &src, const xtd::ustring &dest)
Moves a specified file to a new location, providing the option to specify a new file name.
static void set_last_write_time(const xtd::ustring &path, const xtd::date_time &last_write_time)
Sets the date and time that the specified file was last written to.
static void replace(const xtd::ustring &source_file_name, const xtd::ustring &destination_file_name, const xtd::ustring &destination_backup_file_name)
Replaces the contents of a specified file with the contents of another file, deleting the original fi...
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 xtd::date_time get_last_write_time(const xtd::ustring &path)
Returns the date and time the specified file or directory was last written to.
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,...
Definition file.h:77
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,...
Definition file.h:330
static xtd::io::file_attributes get_attributes(const xtd::ustring &path)
Gets the xtd::io::file_attributes of the file on the path.
static std::ofstream write_text(const xtd::ustring &path)
Creates a std::ofstream that appends text to an existing file.
static void remove(const xtd::ustring &path)
Deletes the specified file.
static std::fstream open(const xtd::ustring &path, std::ios::openmode mode)
Opens a FileStream on the specified path.
static void set_last_access_time(const xtd::ustring &path, const xtd::date_time &last_access_time)
Sets the date and time the specified file was last accessed.
static void move(const xtd::ustring &src, const xtd::ustring &dest, bool overwrite)
Moves a specified file to a new location, providing the options to specify a new file name and to ove...
static void copy(const xtd::ustring &src, const xtd::ustring &dest, bool overwrite)
Copies an existing file to a new file. Overwriting a file of the same name is allowed.
static void set_permissions(const xtd::ustring &path, xtd::io::file_permissions permissions)
Sets the specified xtd::io::file_permissions of the file on the specified path.
static std::ofstream create_text(const xtd::ustring &path)
Creates or opens a file for writing text.
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
static std::ofstream open_write(const xtd::ustring &path)
Opens an existing file for writing.
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,...
Definition file.h:344
static std::ifstream open_text(const xtd::ustring &path)
Opens an existing file for reading.
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 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,...
Definition file.h:357
static void set_attributes(const xtd::ustring &path, xtd::io::file_attributes attributes)
Sets the specified xtd::io::file_attributes of the file on the specified path.
static std::vector< xtd::byte > read_all_bytes(const xtd::ustring &path)
Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
static void copy(const xtd::ustring &src, const xtd::ustring &dest)
Copies an existing file to a new file. Overwriting a file of the same name is not allowed.
static void set_creation_time(const xtd::ustring &path, const xtd::date_time &creation_time)
Sets the date and time the file was created.
static xtd::ustring read_all_text(const xtd::ustring &path)
Opens a text file, reads all text of the file, and then closes the file.
static std::ofstream create(const xtd::ustring &path)
Creates or overwrites a file in the specified path.
static xtd::date_time get_creation_time(const xtd::ustring &path)
Returns the creation date and time of the specified file or directory.
static xtd::io::file_permissions get_permissions(const xtd::ustring &path)
Gets the xtd::io::file_permissions of the file on the path.
static std::vector< xtd::ustring > read_all_lines(const xtd::ustring &path)
Opens a text file, reads all lines of the file, and then closes the file.
static bool exists(const xtd::ustring &path) noexcept
Determines whether the specified file exists.
static xtd::date_time get_last_access_time(const xtd::ustring &path)
Returns the date and time the specified file or directory was last accessed.
static std::ofstream append_text(const xtd::ustring &path)
Creates a std::ofstream that appends text to an existing file.
Performs operations on std::basic_string instances that contain file or directory path information....
Definition path.h:34
Implements a xtd::io::text_writer for writing characters to a stream.
Definition stream_writer.h:26
void write(const xtd::ustring &value) override
Writes the specified string value to the text stream.
void write_line()
Writes new line to the text stream.
Represents text as a sequence of UTF-8 code units.
Definition ustring.h:47
Contains xtd::io::file_attributes enum class.
Contains xtd::io::file_permissions enum class.
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.h:37
#define core_export_
Define shared library export.
Definition core_export.h:13
file_attributes
Provides attributes for files and directories.
Definition file_attributes.h:22
file_permissions
Provides permissions for files and directories.
Definition file_permissions.h:22
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Contains xtd::io::stream_reader class.
Contains xtd::io::stream_writer class.