• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > java文件操作之Path,Paths,Files

java文件操作之Path,Paths,Files

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了java.nio.file.paths,java paths,java paths.get,paths,paths of the soul等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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,自己查看方法名,很容易就能分辨出功能。
 }
</div>

希望本篇文章对需要的朋友有帮助

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • java文件操作之Path,Paths,Files
  • java文件操作之Path,Paths,Files

相关文章

  • 2017-05-28Java vector的详解及实例
  • 2017-05-28Java微信公众平台开发(6) 微信开发中的token获取
  • 2017-05-28java SpringMVC学习使用详解
  • 2017-05-28spring mvc实现登录账号单浏览器登录
  • 2017-05-28微信开发准备第二步 springmvc mybatis项目结构搭建
  • 2017-05-28Spring Boot 快速入门指南
  • 2017-05-28Java实现的自定义迭代器功能示例
  • 2017-05-28详解Servlet 3.0/3.1 中的异步处理
  • 2017-05-28springMVC的生命周期详解
  • 2017-05-28java模拟微信抢红包的实例代码

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Java求字符串中出现次数最多的字符串以及出现次数
    • Java 中Timer和TimerTask 定时器和定时任务使用的例子
    • SpringBoot的服务注册与发现示例
    • 详解Spring Boot中Controller用法
    • java 创建线程的方法总结
    • java多线程编程技术详解和实例代码
    • Windows下Java环境变量配置详解
    • Java中List Set和Map之间的区别_动力节点Java学院整理
    • Java数组的特性_动力节点Java学院整理
    • java 实现定时的方法及实例代码

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有