IO模块-其他操作
copy
File file = new File(getFilesDir(), "test.txt");
File file2 = new File(getFilesDir(), "test2.txt");
try {
Files.copy(file, file2);
} catch (IOException e) {
e.printStackTrace();
}
equal
比较两个文件是否相等。
源码:
public static boolean equal(File file1, File file2) throws IOException {
checkNotNull(file1);
checkNotNull(file2);
if (file1 == file2 || file1.equals(file2)) {
return true;
}
/*
* Some operating systems may return zero as the length for files denoting system-dependent
* entities such as devices or pipes, in which case we must fall back on comparing the bytes
* directly.
*/
long len1 = file1.length();
long len2 = file2.length();
if (len1 != 0 && len2 != 0 && len1 != len2) {
return false;
}
return asByteSource(file1).contentEquals(asByteSource(file2));
}
public boolean contentEquals(ByteSource other) throws IOException {
checkNotNull(other);
byte[] buf1 = createBuffer();
byte[] buf2 = createBuffer();
Closer closer = Closer.create();
try {
InputStream in1 = closer.register(openStream());
InputStream in2 = closer.register(other.openStream());
while (true) {
int read1 = ByteStreams.read(in1, buf1, 0, buf1.length);
int read2 = ByteStreams.read(in2, buf2, 0, buf2.length);
if (read1 != read2 || !Arrays.equals(buf1, buf2)) {
return false;
} else if (read1 != buf1.length) {
return true;
}
}
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
hash
计算文件的hash值。具体的参考guava源码分析关于hash模块的分析。
map
得到文件的内存映射缓冲区。
touch
创建新文件,或者在文件已经存在的情况下更新文件的时间。
...