java文件操作之Path,Paths,Files
程序员文章站
2024-02-29 16:43:04
java7中文件io发生了很大的变化,专门引入了很多新的类:
import java.nio.file.directorystream;
import java.nio...
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; /** * @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; /** * 构建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(); } /** * 创建一个空文件/文件夹 * @throws ioexception */ @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 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));//最后修改时间 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 } /** * 拿到文件输入流 * @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(); } /** * 遍历一个文件夹 */ @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());//上级目录 // system.out.println(path1.getfilesystem());//文件系统 }); //还有种方式files.walkfiletree() } /** * 文件复制 */ @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); } /** * 读取权限见上面示例,设置权限 */ @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,自己查看方法名,很容易就能分辨出功能。 }
希望本篇文章对需要的朋友有帮助