打开和关闭文件
import core.stdc.errno;
import std.exception;
import std.stdio;
void main(string[] args)
{
try
{
auto file = File("test.txt", "r");
file.close();
}
catch (ErrnoException ex)
{
switch(ex.errno)
{
case EPERM:
case EACCES:
break;
case ENOENT:
break;
default:
break;
}
}
}
文件中查找位置
import std.exception;
import std.stdio;
void main(string[] args)
{
try
{
auto file = File("test.txt", "r");
file.seek(10, SEEK_SET);
file.seek(-2, SEEK_CUR);
file.seek(-4, SEEK_END);
auto pos = file.tell();
file.rewind();
}
catch (ErrnoException ex)
{
}
}
写入字节
import std.exception;
import std.stdio;
void main(string[] args)
{
try
{
byte[] data = [0x68, 0x65, 0x6c, 0x6c, 0x6f];
auto file = File("test.txt", "w");
file.rawWrite(data);
}
catch (ErrnoException ex)
{
}
}
快速写入文件
import std.file;
void main(string[] args)
{
try
{
write("test.txt", [0x68, 0x65, 0x6c, 0x6c, 0x6f]);
}
catch (FileException ex)
{
}
}
写行进文件
import std.exception;
import std.stdio;
void main(string[] args)
{
try
{
auto file = File("test.txt", "w");
file.write("1: Lorem ipsum\n");
file.writeln("2: Lorem ipsum");
file.writef("3: %s", "Lorem ipsum\n");
file.writefln("4: %s", "Lorem ipsum");
}
catch (ErrnoException ex)
{
}
}
使用io缓冲.
import std.file;
import std.outbuffer;
void main(string[] args)
{
auto buffer = new OutBuffer();
ubyte[] data = [0x68, 0x65, 0x6c, 0x6c, 0x6f];
buffer.write(data);
buffer.write(' ');
buffer.write("world");
try
{
write("test.txt", buffer.toBytes());
}
catch (FileException ex)
{
}
}
从文件中读取字节
import std.exception;
import std.stdio;
void main(string[] args)
{
try
{
byte[] buffer;
buffer.length = 1024;
auto file = File("test.txt", "r");
auto data = file.rawRead(buffer);
}
catch (ErrnoException ex)
{
}
}
快速读取文件
import std.file;
void main(string[] args)
{
try
{
auto data = cast(byte[]) read("test.txt");
}
catch (FileException ex)
{
}
}
读取N字节
import std.file;
void main(string[] args)
{
try
{
auto data = cast(byte[]) read("test.txt", 5);
}
catch (FileException ex)
{
}
}
分块读文件
import std.exception;
import std.stdio;
void main(string[] args)
{
try
{
auto file = File("test.txt", "r");
foreach (buffer; file.byChunk(1024))
{
}
}
catch (ErrnoException ex)
{
}
}
文件中读取行
import std.exception;
import std.stdio;
void main(string[] args)
{
try
{
auto file = File("test.txt", "r");
string line;
while ((line = file.readln()) !is null)
{
}
}
catch (ErrnoException ex)
{
}
}
缓冲为参的重载
import std.exception;
import std.stdio;
void main(string[] args)
{
try
{
auto file = File("test.txt", "r");
char[] buffer;
while (file.readln(buffer))
{
}
}
catch (ErrnoException ex)
{
}
}
按系列行读取文件
import std.exception;
import std.stdio;
void main(string[] args)
{
try
{
auto file = File("test.txt", "r");
foreach (line; file.byLine)
{
}
}
catch (ErrnoException ex)
{
}
}
文件读为行
import std.file;
import std.utf;
void main(string[] args)
{
try
{
auto utf8Data = readText("test.txt");
auto utf16Data = readText!(wstring)("test.txt");
auto utf32Data = readText!(dstring)("test.txt");
}
catch (UTFException ex)
{
}
catch (FileException ex)
{
}
}
创建空文件
import std.exception;
void main(string[] args)
{
try
{
File("test.txt", "w");
}
catch (ErrnoException ex)
{
}
}
检查是否存在文件
import std.file;
void main(string[] args)
{
if (exists("test.txt"))
{
}
}
重命名和移动文件
import std.file;
void main(string[] args)
{
try
{
rename("source.txt", "destination.txt");
}
catch (FileException ex)
{
}
}
复制文件
import std.file;
void main(string[] args)
{
try
{
copy("source.txt", "destination.txt");
}
catch (FileException ex)
{
}
}
删除文件
import std.file;
void main(string[] args)
{
try
{
remove("test.txt");
}
catch (FileException ex)
{
}
}
获取文件信息
import std.file;
import std.stdio : writefln;
void main(string[] args)
{
try
{
auto file = DirEntry("test.txt");
writefln("名: %s", file.name);
writefln("是目录: %s", file.isDir);
writefln("是文件: %s", file.isFile);
writefln("是符号链接: %s", file.isSymlink);
writefln("大小: %s", file.size);
writefln("最后访问时间: %s", file.timeLastAccessed);
writefln("最后修改时间: %s", file.timeLastModified);
writefln("属性: %b", file.attributes);
}
catch (FileException ex)
{
}
}
截断现有文件
import std.file;
void main(string[] args)
{
auto file = "test.txt";
auto size = 100;
try
{
if (file.exists() && file.isFile())
{
write(file, read(file, size));
}
}
catch (FileException ex)
{
}
}
创建zip文档
import std.file;
import std.outbuffer;
import std.string;
import std.zip;
void main(string[] args)
{
try
{
auto file = new ArchiveMember();
file.name = "test.txt";
auto data = new OutBuffer();
data.write("Lorem ipsum");
file.expandedData = data.toBytes();
auto zip = new ZipArchive();
zip.addMember(file);
write("test.zip", zip.build());
}
catch (ZipException ex)
{
}
}
读取zip文档
import std.file;
import std.zip;
void main(string[] args)
{
try
{
auto zip = new ZipArchive(read("test.zip"));
foreach (filename, member; zip.directory)
{
auto data = zip.expand(member);
}
}
catch (ZipException ex)
{
}
}
写压缩
import std.file;
import std.zlib;
void main(string[] args)
{
try
{
auto data = compress("Lorem ipsum dolor sit amet");
write("test.dat", data);
}
catch (ZlibException ex)
{
}
}
读压缩
import std.file;
import std.zlib;
void main(string[] args)
{
try
{
auto data = uncompress(read("test.dat"));
}
catch (ZlibException ex)
{
}
}
posix
import core.stdc.errno;
import core.sys.posix.sys.stat;
import std.conv;
import std.string;
void main(string[] args)
{
auto file = "test.txt";
auto result = chmod(file.toStringz(), octal!(666));
if (result != 0)
{
switch(errno)
{
case EPERM:
case EACCES:
break;
case ENOENT:
break;
default:
break;
}
}
}
更改文件的所有者
import core.stdc.errno;
import core.sys.posix.pwd;
import core.sys.posix.unistd;
import std.string;
void main(string[] args)
{
auto username = "gary";
auto file = "test.txt";
auto record = getpwnam(username.toStringz());
if (record !is null)
{
auto user = record.pw_uid;
auto group = record.pw_gid;
auto result = chown(file.toStringz(), user, group);
if (result != 0)
{
switch(errno)
{
case EPERM:
break;
default:
break;
}
}
}
}
创建硬链接和符号链接
import core.stdc.errno;
import core.sys.posix.unistd;
import std.string;
void main(string[] args)
{
auto file = "test.txt";
auto linked = "link.txt";
auto result = link(file.toStringz(), linked.toStringz());
if (result != 0)
{
switch(errno)
{
case EPERM:
case EACCES:
break;
case EEXIST:
break;
case ENOENT:
default:
break;
}
}
}
创建符号链接
auto result = link(file.toStringz(), linked.toStringz());
字符串
auto result = symlink(file.toStringz(), linked.toStringz());