java文件操作之Path,Paths,Files
Java7中文件IO發(fā)生了很大的變化,專門引入了很多新的類:
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構(gòu)造一個(gè)文件,然后將File作為入?yún)?,獲取輸入流等操作。Api的操作不是很流暢。在新的文件IO中,用Path取代了File,用Files作為一個(gè)操作類,并且封裝了很多非常實(shí)用的Api,看完下面的示例,就會(huì)有一個(gè)新的理解。
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; /** * 構(gòu)建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(); } /** * 創(chuàng)建一個(gè)空文件/文件夾 * @throws IOException */ @Test public void create() throws IOException { //文件夾 Path path = Paths.get("/Users/kingboy/Desktop/hello"); if(!Files.exists(path)){ Files.createDirectory(path); //創(chuàng)建多個(gè)目錄 //Files.createDirectories(path); } //文件 Path path1 = Paths.get("/Users/kingboy/Desktop/helloFile"); if(Files.exists(path1)){ Files.createFile(path1); } } /** * 文件屬性 */ @Test public void getFileProperties() throws IOException { Path path = Paths.get("/Users/kingboy/Desktop/text.txt"); System.out.println(Files.getLastModifiedTime(path));//最后修改時(shí)間 System.out.println(Files.getOwner(path));//擁有者 System.out.println(Files.getPosixFilePermissions(path));//權(quán)限 System.out.println(Files.size(path));//文件大小 } /** * 讀取一個(gè)文本文件 */ @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)); //輸出結(jié)果 //adsfasdfasdfadsfasdfgsdfsdffsdfsdf //adsfasdfasdfadsfasdfgsdfsdffsdfsdf } /** * 拿到文件輸入流 * @throws IOException */ @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(); } /** * 遍歷一個(gè)文件夾 */ @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(path1 -> { // System.out.println(path1.getRoot());//根目錄 System.out.println(path1.getFileName());//文件名 // System.out.println(path1.getParent());//上級(jí)目錄 // System.out.println(path1.getFileSystem());//文件系統(tǒng) }); //還有種方式Files.walkFileTree() } /** * 文件復(fù)制 */ @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); } /** * 讀取權(quán)限見上面示例,設(shè)置權(quán)限 */ @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,自己查看方法名,很容易就能分辨出功能。 }
希望本篇文章對(duì)需要的朋友有幫助
相關(guān)文章
java 輸入3個(gè)數(shù)a,b,c,按大小順序輸出的實(shí)例講解
今天小編就為大家分享一篇java 輸入3個(gè)數(shù)a,b,c,按大小順序輸出的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07Springboot jpa @Column命名大小寫問題及解決
這篇文章主要介紹了Springboot jpa @Column命名大小寫問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10java.sql.SQLRecoverableException關(guān)閉的連接異常問題及解決辦法
當(dāng)數(shù)據(jù)庫連接池中的連接被創(chuàng)建而長時(shí)間不使用的情況下,該連接會(huì)自動(dòng)回收并失效,就導(dǎo)致客戶端程序報(bào)“ java.sql.SQLException: Io 異常: Connection reset” 或“java.sql.SQLException 關(guān)閉的連接”異常問題,下面給大家分享解決方案,一起看看吧2024-03-03Spring Task定時(shí)任務(wù)每天零點(diǎn)執(zhí)行一次的操作
這篇文章主要介紹了Spring Task定時(shí)任務(wù)每天零點(diǎn)執(zhí)行一次的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09@PreAuthorize、@PostAuthorize、@PreFilter、@PostFilter注解的用法詳解
這篇文章主要介紹了@PreAuthorize、@PostAuthorize、@PreFilter、@PostFilter注解的用法詳解,通過在方法上添加@PreAuthorize注解,可以指定需要滿足的權(quán)限條件,只有滿足條件的用戶才能執(zhí)行該方法,需要的朋友可以參考下2023-10-10SpringBoot項(xiàng)目微信云托管入門部署實(shí)踐
本文主要介紹了SpringBoot項(xiàng)目微信云托管入門部署實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03