Java基础——Path、File、Files

Path、File、Files是什么?

Path:用于操作路径
File:用于操作文件
Files:文件工具类

Path

创建Path

可使用静态Paths.get()创建相对/绝对路径,路径并不需要真实存在,若路径不合法则抛InvalidPathException

Path path = Paths.get("/storage/emulated/0/Android/data/com.demo.demo0/files/Download","data.txt");
System.out.println(path);

会默认使用当前文件系统路径分隔符连接路径(Linux是/,windows是\)

File

Files

读文件

读取文件所有内容,参数为Path

Path path = Paths.get("/storage/emulated/0/Android/data/com.demo.demo0/files/Download","data.txt");
try {
    byte[] bytes = Files.readAllBytes(path);
    System.out.println(new String(bytes));
} catch (IOException e) {
    e.printStackTrace();
}

按行读取文件内容

Path path = Paths.get("/storage/emulated/0/Android/data/com.demo.demo0/files/Download","data.txt");
try {
    List<String> list = Files.readAllLines(path);
    System.out.println(list);
} catch (IOException e) {
    e.printStackTrace();
}

写文件

写文件,参数为Path和byte[]

Path path = Paths.get("/storage/emulated/0/Android/data/com.demo.demo0/files/Download","data.txt");
try {
    Files.write(new File(path ,"0".getBytes());
} catch (IOException e) {
    e.printStackTrace();
}

如要对文件追加内容,则加入参数StandardOpenOption.APPEND

Path path = Paths.get("/storage/emulated/0/Android/data/com.demo.demo0/files/Download","data.txt");
try {
    Files.write(path, "0".getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
    e.printStackTrace();
}

从Files提取流

InputStream inputStream = Files.newInputStream(path);
OutputStream outputStream = Files.newOutputStream(path);
BufferedReader bufferedReader = Files.newBufferedReader(path);
BufferedWriter bufferedWriter = Files.newBufferedWriter(path);

创建目录

除最后一个路径外,前边路径必须是已存在的

Path path = Paths.get("/storage/emulated/0/Android/data/com.demo.demo0/files/Download/dir");
try {
    Files.createDirectory(path);
} catch (IOException e) {
    e.printStackTrace();
}

创建文件

如果文件已经存在,则抛出异常,

Path path = Paths.get("/storage/emulated/0/Android/data/com.demo.demo0/files/Download/1.txt");
try {
    Files.createFile(path);
} catch (IOException e) {
    e.printStackTrace();
}

复制、剪切文件

复制调用copy,若要覆盖,则可添加参数StandardCopyOption.REPLACE_EXISTING,StandardCopyOption.COPY_ATTRIBUTES

Path form = Paths.get("/storage/emulated/0/Android/data/com.demo.demo0/files/Download/1.txt");
Path to = Paths.get("/storage/emulated/0/Android/data/com.demo.demo0/files/Download/dir/1.txt");
try {
    Files.copy(form, to);
} catch (IOException e) {
    e.printStackTrace();
}

剪切调用move,若要保证剪切的原子性,可添加参数StandardCopyOption.ATOMIC_MOVE

Path form = Paths.get("/storage/emulated/0/Android/data/com.demo.demo0/files/Download/1.txt");
Path to = Paths.get("/storage/emulated/0/Android/data/com.demo.demo0/files/Download/dir/1.txt");
try {
    Files.move(form, to);
} catch (IOException e) {
    e.printStackTrace();
}

删除文件

删除调用delete,若文件不存在则抛出异常,用deleteIfExists可避免异常,还可用于删除空目录

Path path = Paths.get("/storage/emulated/0/Android/data/com.demo.demo0/files/Download/dir/1.txt");
try {
    Files.delete(path);
} catch (IOException e) {
    e.printStackTrace();
}

获取文件信息

是否存在、是否隐藏、是否可读可写可执行、是否目录、是否普通文件、是否软连接

boolean exists = Files.exists(path);
boolean hidden = Files.isHidden(path);
boolean readable = Files.isReadable(path);
boolean writable = Files.isWritable(path);
boolean executable = Files.isExecutable(path);
boolean directory = Files.isDirectory(path);
boolean regularFile = Files.isRegularFile(path);
boolean symbolicLink = Files.isSymbolicLink(path);

获取大小、拥有者、创建时间等

long size = Files.size(path);

UserPrincipal owner = Files.getOwner(path);
String name = owner.getName();

BasicFileAttributes basicFileAttributes = Files.readAttributes(path, BasicFileAttributes.class);
FileTime fileTime = basicFileAttributes.creationTime();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值