java 对于java.nio.file.Files
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 对于java.nio.file.Files相关的知识,希望对你有一定的参考价值。
import org.junit.Test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.EnumSet;
import java.util.List;
/**
* @author piumnl
* @version 1.0.0
* @since on 2016-12-29.
*/
public class FilesDemo {
@Test
public void test_readAllBytes() throws IOException {
byte[] bytes = Files.readAllBytes(Paths.get("E:", "xuan.png"));
Path png = Paths.get("E:", "m.png" );
Files.deleteIfExists(png);
Files.write(png, bytes, StandardOpenOption.CREATE_NEW);
}
@Test
public void test_readAllLines() throws IOException {
String userDir = System.getProperty("user.dir");
Path imlFile = Paths.get(userDir, "io.iml");
List<String> strings = Files.readAllLines(imlFile, Charset.forName("UTF-8"));
Path path = Paths.get(userDir, "write.iml");
if (Files.notExists(path)) {
Files.createFile(path);
}
Files.write(path, strings, Charset.forName("UTF-8"));
}
@Test
public void test_stream() throws IOException {
Path inPath = Paths.get(System.getProperty("user.dir"), "io.iml");
Path outPath = Paths.get(System.getProperty("user.dir"), "write.iml");
InputStream inputStream = Files.newInputStream(inPath, StandardOpenOption.APPEND);
OutputStream outputStream = Files.newOutputStream(outPath);
BufferedReader bufferedReader = Files.newBufferedReader(inPath, Charset.forName("UTF-8"));
BufferedWriter bufferedWriter = Files.newBufferedWriter(outPath, Charset.forName("UTF-8"));
}
@Test
public void test_copy() throws IOException {
Files.copy(Paths.get(System.getProperty("user.dir"), "io.iml"),
Paths.get(System.getProperty("user.dir"), "io_copy.iml"),
StandardCopyOption.REPLACE_EXISTING);
}
@Test
public void test_move() throws IOException {
Files.move(Paths.get(System.getProperty("user.dir"), "io_copy.iml"),
Paths.get(System.getProperty("user.dir"), "mm"));
}
@Test
public void test_delete() throws IOException {
// Files.delete(Paths.get(System.getProperty("user.dir"), "mm"));
Files.deleteIfExists(Paths.get(System.getProperty("user.dir"), "mm"));
}
@Test
public void test_createDirectory() throws IOException {
// Files.createDirectory(Paths.get(System.getProperty("user.dir"), "mm", "xx"));
// Files.createDirectory(Paths.get(System.getProperty("user.dir"), "mm"));
Files.createDirectories(Paths.get(System.getProperty("user.dir"), "mm1", "xx"));
}
@Test
public void test_createFile() throws IOException {
Files.createFile(Paths.get(System.getProperty("user.dir"), "createFile.txt"));
}
@Test
public void test_createTempFile() throws IOException, InterruptedException {
Path path = Paths.get(System.getProperty("user.dir"), "temp");
if (Files.notExists(path))
Files.createDirectory(path);
Files.createTempFile(path, "log", ".txt");
}
@Test
public void test_createTempDirectory() throws IOException {
Path tempPath = Paths.get(System.getProperty("user.dir"), "temp");
if (Files.notExists(tempPath)) {
Files.createDirectory(tempPath);
}
Files.createTempDirectory(tempPath, "xx");
}
@Test
public void test_SymbolLink() throws IOException {
Path link = Paths.get("E:", "scp", "mmm.link");
Files.createSymbolicLink(link, Paths.get(System.getProperty("user.dir")));
}
@Test
public void test_walk() throws IOException {
Path path = Paths.get(System.getProperty("user.dir"));
ListTree listTree = new ListTree();
Files.walkFileTree(path, listTree);
EnumSet<FileVisitOption> followLinks = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
Files.walkFileTree(path, followLinks, Integer.MAX_VALUE, listTree);
}
// NIO2 递归遍历文件目录的接口实现
class ListTree extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
System.out.println("Visited directory: " + dir.toString());
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.out.println(exc);
return FileVisitResult.CONTINUE;
}
}
}
以上是关于java 对于java.nio.file.Files的主要内容,如果未能解决你的问题,请参考以下文章