zoukankan      html  css  js  c++  java
  • Java NIO学习(Path接口、Paths和Files工具类的使用)

    NIO学习:Paths和Files工具类的使用

    JDK1.7引入了新的IO操作类。在java.nio.file包下,Java NIO Path接口和Files类。

    • Path接口:Path表示的是一个目录名序列,其后还可以跟着一个文件名,路径中第一个部件是根部件时就是绝对路径。
    • 例如:/或C:/。而允许访问的根部件取决于文件系统。

    • 以根部件开始的路径是绝对路径,否则就是相对路径。

    • 静态的Paths.get方法接受一个或多个字符串,字符串之间自动使用默认文件系统的路径分隔符连接起来(Unix是/,Windows是),这就解决了跨平台的问题。接着解析连接起来的结果,如果不是合法路径就抛出InvalidPathException异常,否则就返回一个Path对象。

    • Files工具类:提供了更方便更高效的对文件进行读写的操作方法。

    • 1、读写文件

    • static Path write(Path path, byte[] bytes, OpenOption...Options):写入文件

    • static byte[] readAllBytes(Path path):读取文件中的所有字节。

    代码示例

    import java.io.File;
    import java.io.IOException;
    import java.nio.file.FileSystems;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.StandardCopyOption;
    import java.nio.file.StandardOpenOption;
    
    
    public class PathFilesDemo {
    	public static void main(String[] args) {
    		createFileOrDir();
    	}
    	
    	// 创建文件或目录
    	private static void createFileOrDir() {
    		try {
    			// 创建新目录,除了最后一个部件,其他必须是存在的
    			Files.createDirectory(Paths.get("F:/test"));
    
    			// 创建路径中的中间目录,能创建不存在的中间部件
    			Files.createDirectories(Paths.get("F:/test/test"));
    
    			// 创建文件
    			Files.createFile(Paths.get("F:/testbak.txt"));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}// createFileOrDir
    
    	// 删除文件
    	private static void deleteFile() {
    		Path p = Paths.get("F:/test.txt");
    		try {
    			Files.delete(p);// 用static boolean deleteIfExists(Path path)方法比较好
    			System.out.println("删除成功");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}// deleteFile
    
    	// 移动文件
    	private static void moveFile() {
    		Path pSrc = Paths.get("F:/test.txt");
    		Path pDest = Paths.get("E:/test.txt");
    		try {
    			Files.move(pSrc, pDest, StandardCopyOption.REPLACE_EXISTING);
    			System.out.println("移动成功");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}// moveFile
    
    	// 复制文件
    	private static void copyFile() {
    		Path pSrc = Paths.get("F:/test.txt");
    		Path pDest = Paths.get("F:/testbak.txt");
    		try {
    			Files.copy(pSrc, pDest, StandardCopyOption.REPLACE_EXISTING);
    			System.out.println("复制成功");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}// copyFile
    
    	// 从文件读取数据
    	private static void readFromFile() {
    		Path p = Paths.get("F:/", "test.txt");
    		try {
    			byte[] bytes = Files.readAllBytes(p);
    			System.out.println(new String(bytes));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	// 向文件写入数据
    	private static void write2File() {
    		// 获得路径
    		Path p = Paths.get("F:/", "test.txt");
    		String info = "I love java really,你喜欢什么?";
    		try {
    			// 向文件中写入信息
    			Files.write(p, info.getBytes("utf8"), StandardOpenOption.APPEND);
    			System.out.println("写入成功");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}// write2File
    
    	// 获得文件路径的几种方法
    	private static void getFilePath() {
    		File file = new File("F:/test.txt");
    		// Path
    		Path p1 = Paths.get("F:/", "test.txt");// F:	est.txt
    		System.out.println(p1);
    
    		Path p2 = file.toPath();
    		System.out.println(p2);
    
    		Path p3 = FileSystems.getDefault().getPath("F:/", "test.txt");
    		System.out.println(p3);
    	}// getFilePath
    }
  • 相关阅读:
    (DT系列五)Linux kernel 是怎么将 devicetree中的内容生成plateform_device【转】
    Linux 内核虚拟地址到物理地址转换讨论【转】
    【Go命令教程】6. go doc 与 godoc
    【Go命令教程】5. go clean
    【Go命令教程】4. go get
    PHP项目收藏
    【Go命令教程】3. go install
    分享下使用 svn,测试服务器代码自动更新、线上服务器代码手动更新的配置经验
    【Go命令教程】2. go build
    【Go命令教程】1. 标准命令详解
  • 原文地址:https://www.cnblogs.com/zxfei/p/10901364.html
Copyright © 2011-2022 走看看