java file operation Path Paths Files
- 2020-06-23 00:22:46
- OfStack
Java7中文件IO发生了很大的变化,专门引入了很多新的类:
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
In the previous operation, a file is constructed mainly through File, and then File is taken as input parameter to obtain input stream and other operations. The Api doesn't operate very smoothly. In the new file IO, Path replaces File, USES Files as an action class, and encapsulates many of the very useful Api. Look at the following examples and you will see a new understanding.
package filespaths;
import org.junit.Test;
import java.io.*;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
/**
* @Author kingboy
* @Date 2017/4/13 11:05
* @Description Path is used to Path Sample
* @email kingboyworld@163.com
*/
public class PathTest {
private static String separator = File.separator;
/**
* build Path
*/
@Test
public void constructon(){
//1.Paths
Path path = Paths.get("/Users/kingboy/Desktop/");
Path path1 = Paths.get(URI.create("/Users/kingboy/Desktop/"));
//2.FileSystems
Path path2 = FileSystems.getDefault().getPath("/Users/kingboy/Desktop/");
//3.File
Path path3 = new File("/Users/kingboy/Desktop/").toPath();
}
/**
* create 1 An empty file / folder
* @throws IOException
*/
@Test
public void create() throws IOException {
// folder
Path path = Paths.get("/Users/kingboy/Desktop/hello");
if(!Files.exists(path)){
Files.createDirectory(path);
// Create multiple directories
//Files.createDirectories(path);
}
// file
Path path1 = Paths.get("/Users/kingboy/Desktop/helloFile");
if(Files.exists(path1)){
Files.createFile(path1);
}
}
/**
* File attributes
*/
@Test
public void getFileProperties() throws IOException {
Path path = Paths.get("/Users/kingboy/Desktop/text.txt");
System.out.println(Files.getLastModifiedTime(path));// Last modified time
System.out.println(Files.getOwner(path));// The owner
System.out.println(Files.getPosixFilePermissions(path));// permissions
System.out.println(Files.size(path));// The file size
}
/**
* read 1 Text file
*/
@Test
public void readText() throws IOException {
Path path = Paths.get("/Users/kingboy/Desktop/text.txt");
// through bufferedReader read
BufferedReader bufferedReader = Files.newBufferedReader(path, StandardCharsets.UTF_8);// File coding
StringBuilder sb = new StringBuilder();
String tempString = null;
while ((tempString = bufferedReader.readLine())!=null){
sb = sb.append(tempString);
}
System.out.println(sb);
// through Files methods readAllLines
List<String> strings = Files.readAllLines(path);
strings.forEach(s -> System.out.print(s));
// The output
//adsfasdfasdfadsfasdfgsdfsdffsdfsdf
//adsfasdfasdfadsfasdfgsdfsdffsdfsdf
}
/**
* Get the file input stream
* @throws IOException
*/
@Test
public void getInputStream() throws IOException {
Path path = Paths.get("/Users/kingboy/Desktop/text.txt");
InputStream inputStream = Files.newInputStream(path);
}
/**
* File write operation
*/
@Test
public void writeFile() throws IOException {
Path path = Paths.get("/Users/kingboy/Desktop/writeFile");
BufferedWriter bufferedWriter = Files.newBufferedWriter(path);
String str = "write file test";
bufferedWriter.write(str);
bufferedWriter.flush();
bufferedWriter.close();
}
/**
* traverse 1 A folder
*/
@Test
public void traverseDirectory() throws IOException {
Path path = Paths.get("/Users/kingboy/Desktop/");
Stream<Path> list = Files.list(path);
list.forEach(p -> {
System.out.println(p.getFileName());
});
}
/**
* Traverse the file tree
*/
@Test
public void traverseTree() throws IOException {
Path path = Paths.get("/Users/kingboy/Desktop/");
Stream<Path> walk = Files.walk(path);
walk.forEach(path1 -> {
// System.out.println(path1.getRoot());// The root directory
System.out.println(path1.getFileName());// The file name
// System.out.println(path1.getParent());// The higher the directory
// System.out.println(path1.getFileSystem());// The file system
});
// There's another way Files.walkFileTree()
}
/**
* Copying files
*/
@Test
public void copyFile() throws IOException {
Path path = Paths.get("/Users/kingboy/Desktop/text.txt");
Path path2 = Paths.get("/Users/kingboy/Desktop/hello.txt");
Files.copy(path,path2);
}
/**
* Read permissions are shown in the example above. Set permissions
*/
@Test
public void writePermission() throws IOException {
Path path = Paths.get("/Users/kingboy/Desktop/text.txt");
Set<PosixFilePermission> permissionSet = new HashSet<>();
permissionSet.add(PosixFilePermission.GROUP_WRITE);
permissionSet.add(PosixFilePermission.OWNER_EXECUTE);
Files.setPosixFilePermissions(path,permissionSet);
}
// There are many other operations Api, Look at the method name for yourself and it's easy to tell the function apart.
}
I hope this article is helpful to those in need