这篇文章将为大家详细讲解有关java实现操作文件的方法有哪些,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
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;
在以前的操作中,主要通过File构造一个文件,然后将File作为入参,获取输入流等操作。Api的操作不是很流畅。在新的文件IO中,用Path取代了File,用Files作为一个操作类,并且封装了很多非常实用的Api,看完下面的示例,就会有一个新的理解。
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;public class PathTest { private static String separator = File.separator; @Test public void constructon(){ //1.Paths Path path = Paths.get("/Users/kingboy/Desktop/"); Path path2 = Paths.get(URI.create("/Users/kingboy/Desktop/")); //2.FileSystems Path path3 = FileSystems.getDefault().getPath("/Users/kingboy/Desktop/"); //3.File Path path4 = new File("/Users/kingboy/Desktop/").toPath(); } @Test public void create() throws IOException { //文件夹 Path path = Paths.get("/Users/kingboy/Desktop/hello"); if(!Files.exists(path)){ Files.createDirectory(path); //创建多个目录 //Files.createDirectories(path); } //文件 Path path2 = Paths.get("/Users/kingboy/Desktop/helloFile"); if(Files.exists(path2)){ Files.createFile(path2); } } @Test public void getFileProperties() throws IOException { Path path = Paths.get("/Users/kingboy/Desktop/text.txt"); System.out.println(Files.getLastModifiedTime(path));//最后修改时间 System.out.println(Files.getOwner(path));//拥有者 System.out.println(Files.getPosixFilePermissions(path));//权限 System.out.println(Files.size(path));//文件大小 } @Test public void readText() throws IOException { Path path = Paths.get("/Users/kingboy/Desktop/text.txt"); //通过bufferedReader读取 BufferedReader bufferedReader = Files.newBufferedReader(path, StandardCharsets.UTF_8);//文件编码 StringBuilder sb = new StringBuilder(); String tempString = null; while ((tempString = bufferedReader.readLine())!=null){ sb = sb.append(tempString); } System.out.println(sb); //通过Files方法readAllLines List<String> strings = Files.readAllLines(path); strings.forEach(s -> System.out.print(s)); //输出结果 //adsfasdfasdfadsfasdfgsdfsdffsdfsdf //adsfasdfasdfadsfasdfgsdfsdffsdfsdf } @Test public void getInputStream() throws IOException { Path path = Paths.get("/Users/kingboy/Desktop/text.txt"); InputStream inputStream = Files.newInputStream(path); } @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(); } @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()); }); } @Test public void traverseTree() throws IOException { Path path = Paths.get("/Users/kingboy/Desktop/"); Stream<Path> walk = Files.walk(path); walk.forEach(path2 -> {// System.out.println(path2.getRoot());//根目录 System.out.println(path2.getFileName());//文件名// System.out.println(path2.getParent());//上级目录// System.out.println(path2.getFileSystem());//文件系统 }); //还有种方式Files.walkFileTree() } @Test public void copyFile() throws IOException { Path path = Paths.get("/Users/kingboy/Desktop/text.txt"); Path path3 = Paths.get("/Users/kingboy/Desktop/hello.txt"); Files.copy(path,path3); } @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); } //还有很多其他操作Api,自己查看方法名,很容易就能分辨出功能。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756