java1.7引入的新的IO操作方式

本文详细介绍了Java7中新增的Path API,它替代了传统的File类进行文件IO操作。Path API提供了更强大的路径管理和文件处理功能,包括路径的创建、转换、文件的创建等,并展示了多种构造Path对象的方法。

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;

……等等,来取代原来的基于java.io.File的文件IO操作方式.

  1. Path就是取代File的

A Path represents a path that is hierarchical and composed of a sequence of directory and file name elements separated by a special separator or delimiter.

Path用于来表示文件路径和文件。可以有多种方法来构造一个Path对象来表示一个文件路径,或者一个文件:

1)首先是final类Paths的两个static方法,如何从一个路径字符串来构造Path对象:

    Path path = Paths.get("C:/", "Xmp");
    Path path2 = Paths.get("C:/Xmp");

    URI u = URI.create("file:///C:/Xmp/dd");        
    Path p = Paths.get(u);

2)FileSystems构造:

Path path3 = FileSystems.getDefault().getPath(“C:/”, “access.log”);
3)File和Path之间的转换,File和URI之间的转换:

    File file = new File("C:/my.ini");
    Path p1 = file.toPath();
    p1.toFile();
    file.toURI();

4)创建一个文件:

复制代码
Path target2 = Paths.get(“C:\mystuff.txt”);
// Set perms = PosixFilePermissions.fromString(“rw-rw-rw-“);
// FileAttribute

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值