Java学习路线-25:文件操作File类

举报
彭世瑜 发表于 2021/08/13 22:43:24 2021/08/13
【摘要】 第15 章 : 文件操作 67 File类基本操作 文件操作系统操作类: java.io.File 文件创建,删除,重命名 File类基本使用 File 实现了Comparable接口 // 构造方法 public File(String pathname) public File(String parent, String child) // 创建文件 p...

第15 章 : 文件操作

67 File类基本操作

文件操作系统操作类:
java.io.File

文件创建,删除,重命名

File类基本使用
File 实现了Comparable接口

// 构造方法
public File(String pathname)
public File(String parent, String child)

// 创建文件
public boolean createNewFile() throws IOException

// 文件存在
public boolean exists()

// 删除文件
public boolean delete()

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

示例:

import java.io.File;
import java.io.IOException;

class Demo { public static void main(String[] args) throws IOException { File file = new File("./demo.txt"); if(file.exists()){ boolean ret = file.delete(); System.out.println("删除结果:" + ret); } else{ boolean ret =  file.createNewFile(); System.out.println("创建结果:" + ret); } }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

68 File类操作深入

1、路径分隔符
Windows分隔符 "\"
Linux分隔符 "/"
路径分隔符常量 File.separator

2、文件操作流程

程序 -> JVM -> 操作系统函数 -> 文件处理

  
 
  • 1

重复删除或创建的时候会有延时情况,
建议:文件名不要重名

3、创建文件的时候父级目录必须存在

// 获取父路径
public File getParentFile()

// 创建目录
public boolean mkdirs()

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

示例

import java.io.File;
import java.io.IOException;

class Demo { public static void main(String[] args) throws IOException { File file = new File("./dir/demo/demo.txt"); File parentFile = file.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } file.createNewFile(); }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

69 获取文件信息

涉及文件本身操作,不涉及文件内容处理

// 是否可读
public boolean canRead()

// 是否可写
public boolean canWrite()

// 是否可执行
public boolean canExecute()

// 是否为文件
public boolean isFile()

// 是否为目录
public boolean isDirectory()

// 获取文件长度 字节
public long length()

// 最后修改时间 13位时间戳
public long lastModified()

// 列出目录内容
public File[] listFiles()


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

示例

import java.io.File;

class Demo { public static void main(String[] args) { File file = new File("./dir/demo"); System.out.println(file.canRead()); // true System.out.println(file.canWrite());  // true System.out.println(file.canExecute()); // false System.out.println(file.isFile()); // true System.out.println(file.isDirectory());  // false System.out.println(file.length());  // 135 字节 System.out.println(file.lastModified()); // 1574952161000 File[] list = file.listFiles(); for (File f : list) { System.out.println(f); } // ./dir/demo/demo.txt }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

70 综合案例:列出目录结构

import java.io.File;

class Demo { public static void main(String[] args) { File file = new File("./dir"); listDir(file); } public static void listDir(File file){ if(file.isDirectory()){ File[] files = file.listFiles(); if(files != null){ for(File f: files){ listDir(f); } } } else{ System.out.println(file); } }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

71 综合案例:文件批量更名

import java.io.File;

class Demo { public static void main(String[] args) { long start = System.currentTimeMillis(); File file = new File("./dir"); renameDir(file); long end = System.currentTimeMillis(); System.out.println("花费时间:" + (end - start)); } public static void renameDir(File file){ if(file.isDirectory()){ File[] files = file.listFiles(); if(files != null){ for(File f: files){ renameDir(f); } } } else{ if (file.getName().contains(".")){ int endPos = file.getName().lastIndexOf("."); String name = file.getName().substring(0, endPos); File newFile = new File(file.getParent(), name + ".txt"); System.out.println(file.getName()); System.out.println(newFile); file.renameTo(newFile); } } }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。

原文链接:pengshiyu.blog.csdn.net/article/details/103649113

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。