赞
踩
目录
- import java.io.IOException;
-
- //观察 get 系列的特点和差异
-
- public class Demo1 {
- public static void main(String[] args) throws IOException {
- // File f = new File("D:/test.txt");//绝对路径
- File f = new File("./test.txt");//相对路径
- System.out.println(f.getParent());
- System.out.println(f.getName());
- System.out.println(f.getPath());
- System.out.println(f.getAbsolutePath());
- System.out.println(f.getCanonicalPath());
- //绝对路径运行结果
- //D:\
- //test.txt
- //D:\test.txt
- //D:\test.txt
- //D:\test.txt
- //后面三种结果看起来一样,其实不同,如果变换构造File的路径,运行结果不同
-
- //相对路径运行结果
- //.
- //test.txt
- //.\test.txt
- //E:\2022code\Java-language\system_code\.\test.txt
- //E:\2022code\Java-language\system_code\test.txt
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- import java.io.File;
- import java.io.IOException;
-
- public class Demo2 {
- public static void main(String[] args) throws IOException {
- File f = new File("./test.txt");
- //判断文件是否存在,存在就返回true,不存在就返回false
- System.out.println(f.exists()); //false
- //判断类型,是否是个目录
- System.out.println(f.isDirectory()); //false
- //判断是否是个普通文件
- System.out.println(f.isFile()); //false
-
- //创建这个不存在的文件,创建文件会抛出异常
- f.createNewFile();
-
- //判断文件是否存在,存在就返回true,不存在就返回false
- System.out.println(f.exists()); //true
- //判断类型,是否是个目录
- System.out.println(f.isDirectory()); //false
- //判断是否是个普通文件
- System.out.println(f.isFile()); //true
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- //删除文件
- //创建文件成功后在项目目录下会生成一个test.txt文件,删除文件运行后,相应的文件被删除掉。
-
- import java.io.File;
-
- public class Demo3 {
- public static void main(String[] args) {
- File f = new File("./test.txt");
- f.delete();
- }
- }
- //创建目录
- //和删除文件一样,程序不会有任何的提示,创建目录项目会在system_code目录下生成一个目录
-
- import java.io.File;
-
- public class Demo4 {
- public static void main(String[] args) {
- // File f = new File("./testDir");
- File f = new File("./testDir/aaa/bbb");//创建多级目录
- //make directory 创建目录
- f.mkdir();//创建单层目录
- f.mkdirs();//创建多层目录
- }
- }
- //重命名
-
- import java.io.File;
-
- public class Demo5 {
- public static void main(String[] args) {
- File sreFile = new File("aaa.txt");
- File destFile = new File("bbb.txt");
- sreFile.renameTo(destFile);//把文件名从aaa改成bbb
- }
- }
- //list() 返回 File 对象代表的目录下的所有文件名
-
- import java.io.File;
- import java.util.Arrays;
-
- public class Demo6 {
- public static void main(String[] args) {
- File f = new File("./testDir");
- String[] results = f.list();
- System.out.println(Arrays.toString(results));
- //[aaa, bbb, ccc, ddd]
- }
- }
- //读文件
-
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
-
- public class Demo7 {
- public static void main(String[] args) throws IOException {
- //InputStream 是一个抽象类,不具体,不能实例化
- //相当于打开文件操作,要想读文件就必须先打开文件
- InputStream inputStream = new FileInputStream("./bbb.txt");
- while (true) {
- int b = inputStream.read();
- // 读到文件末尾
- if (b == -1) {
- break;
- }
- System.out.println(b);
- }
- //读完必须关闭文件
- inputStream.close();
- }
- }
-
- // 此处使用字节流读取文件的结果是字符的ASCAII值
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- //字符流读文件
- public class Demo2 {
- public static void main(String[] args) throws IOException {
- //使用字符流读文件
- Reader reader = new FileReader("./bbb.txt");
- while (true) {
- int ret = reader.read();
- if(ret == -1) {
- break;
- }
- char ch = (char)ret;
- System.out.println(ch);
- }
- //用完之后要关掉
- reader.close();
- }
- }
-
- // 使用字符流读文件获取到的就是文件本身的字符
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- // 针对文本文件,使用字符流的时候还可以使用Scanner读取文件(简化方法)
- public class Demo4 {
- public static void main(String[] args) throws IOException {
- //使用Scanner 读文本文件
- InputStream inputStream = new FileInputStream("./bbb.txt");
- Scanner scanner = new Scanner(inputStream);
- while (scanner.hasNext()) {
- System.out.println(scanner.next());
- }
- inputStream.close();
- }
- }
- //字节流写文件
- public class Demo1 {
- //进行写文件
- public static void main(String[] args) throws IOException {
- OutputStream outputStream = new FileOutputStream("./bbb.txt");
-
- //使用outputstream写文件的时候,只要打开文件成功,就会把原有的文件内容清空。bbb文件内容变为abc
- //小写字母的ascii码值对应的数字
- outputStream.write(97);
- outputStream.write(98);
- outputStream.write(99);
-
- outputStream.close();
- }
- }
- public class Demo3 {
- public static void main(String[] args) throws IOException {
- //使用字符流来写文件
- Writer writer = new FileWriter("./bbb.txt");
- writer.write("Hello world");
- writer.close();
- }
- }
- // 针对写文本文件,还可以使用 PrintWriter(简化方法)
- public class Demo5 {
- public static void main(String[] args) throws IOException {
- // try with resources 把要关闭的对象写到try()里,当try结束,就会自动的调用到对应对象的close方法。
- // 而且支持一个 () 放多个对象,多个对象的创建之间使用;分割。
- try (OutputStream outputStream = new FileOutputStream("./bbb.txt")){
- PrintWriter writer = new PrintWriter(outputStream);
-
- writer.println();
- writer.print("a = %d\n, 10");
- }
- }
- }
❗❗进行文件操作一定要关闭文件,原因是每个进程都对应着PCB,PCB里有一个字段是文件描述表,同一个进程里多个PCB共用一份文件描述符表,文件描述表就相当于一个数组(最大长度有上限),进程每打开一个文件就会在这个表里创建一个项(数组里的元素),关闭文件就会将这个项释放掉,如果不关闭文件,这个表项就会一直占着位置,持续打开文件且不关闭,就会耗尽表项,后续再打开文件就会打开失败(文件资源泄露,严重性大于程序崩溃)。JVM其实有自动释放策略的,但是需要被GC销毁,自动关闭对应的文件,但是这也是需要你给JVM通知一下的,可能存在隐患。服务器长期持续执行,更担心这种问题的存在。
- //扫描指定目录,并找到名称中包含指定字符的所有普通文件(不包含目录),并且后续询问用户是否要删除该文件
- public class Demo6 {
- public static void main(String[] args) throws IOException {
- // 1.让用户输入必要的信息(要扫描的路径+要查找的词)
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入要扫描的路径:");
- //判定路径是否存在
- File rootDir = new File(scanner.next());
- if (!rootDir.isDirectory()) {
- System.out.println("您输入的目录不存在!");
- return;
- }
- System.out.println("请输入要搜索的关键词:");
- String toDelete = scanner.next();
-
- // 2.遍历目录,找到名字配匹配的文件。
- // 需要借助一个核心方法,listFiles()
- // 能够把当前目录里的文件和子目录列举出来,但是这个方法只能列出一层,没法直接列出子目录中的子目录。
- // 解决方法就是遍历listFiles 的结果,针对每个元素,进行判定,看它是一个普通文件还是一个目录,
- // 如果是普通文件,直接判定文件名是否包含了要查的词,如果是目录,递归调用listFiles。
- scaDir(rootDir,toDelete);
- }
-
- //借助这个方法进行递归遍历,相当于递归遍历n叉树
- private static void scaDir(File rootDir,String toDelete) throws IOException {
- System.out.println("当前访问: " + rootDir.getCanonicalPath());
- File[] files = rootDir.listFiles();
- if (files == null) {
- //说明 rootDir 是一个空的目录
- return;
- }
- //如果目录非空,则循环遍历里面的每个元素
- for (File f : files) {
- if (f.isDirectory()) {
- scaDir(f,toDelete);
- } else {
- //不是根目录,普通文件,判定文件名是否符合要求,是否要进行删除
- checkDelete(f,toDelete);
- }
- }
- }
-
- // 3.询问用户是否删除
- private static void checkDelete(File f, String toDelete) throws IOException {
- if (f.getName().contains(toDelete)) {
- System.out.println("该单词" + toDelete + "被" + f.getCanonicalPath() + "包含了,是否要删除?(Y/N)");
- Scanner scanner = new Scanner(System.in);
- String choice = scanner.next();
- if (choice.equals("Y") || choice.equals("y")) {
- f.delete();
- }
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
升级版(性能较低,查找过程会很漫长):
- //在扫描指定目录的基础上增加了查找内容的功能
-
- public class Demo8 {
- public static void main(String[] args) throws IOException {
- // 1. 输入路径和要查询的关键词
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入要扫描的路径: ");
- File rootDir = new File(scanner.next());
- System.out.println("请输入要查询的词: ");
- String toFind = scanner.next();
-
- // 2. 递归的扫描目录.
- scanDir(rootDir, toFind);
- }
-
- // 递归操作
- private static void scanDir(File rootDir, String toFind) throws IOException {
- File[] files = rootDir.listFiles();
- // 空目录
- if (files == null) {
- return;
- }
- for (File f : files) {
- if (f.isDirectory()) {
- scanDir(f, toFind);
- } else {
- checkFile(f, toFind);
- }
- }
- }
-
- private static void checkFile(File f, String toFind) throws IOException {
- // 1. 先检查文件名
- if (f.getName().contains(toFind)) {
- System.out.println(f.getCanonicalPath() + " 文件名中包含 " + toFind);
- }
- // 2. 再检查文件内容
- try (InputStream inputStream = new FileInputStream(f)) {
- StringBuilder stringBuilder = new StringBuilder();
- Scanner scanner = new Scanner(inputStream);
- // 按行读取
- while (scanner.hasNextLine()) {
- // 把换行拼接到文件中
- stringBuilder.append(scanner.nextLine() + "\n");
- }
- // 判断文件中是否包含要查找的词
- if (stringBuilder.indexOf(toFind) > -1) {
- System.out.println(f.getCanonicalPath() + " 文件内容包含 " + toFind);
- }
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- // 进行普通文件的复制
- // 把第一个文件打开,把里面的内容逐个字节的读取出来,写到第二个文件即可
- // 使用字节流来进行操作,字节流也是可以用来拷贝文本文件的
-
- public class Demo7 {
- public static void main(String[] args) {
- // 1. 先输入需要复制的文件(源文件), 以及需要把这个文件复制到哪里去(目标文件)
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入源文件: ");
- // srcFile : d:/cat.jpg
- File srcFile = new File(scanner.next());
- System.out.println("请输入目标文件: ");
- // destFile : d:/cat2.jpg
- File destFile = new File(scanner.next());
- // 判断源文件是否存在
- if (!srcFile.isFile()) {
- System.out.println("输入源文件错误!");
- return;
- }
- // 判断目标文件是否存在
- if (!destFile.getParentFile().isDirectory()) {
- System.out.println("输入目标文件错误!");
- return;
- }
- // 2. 打开源文件, 按照字节读取内容, 依次写入到目标文件中.
- // 同时打开多个文件
- try (InputStream inputStream = new FileInputStream(srcFile);
- OutputStream outputStream = new FileOutputStream(destFile)) {
- // 读 src 的每个字节, 写入到 dest 中.
- while (true) {
- int ret = inputStream.read();
- if (ret == -1) {
- break;
- }
- // 一边读一边写
- outputStream.write(ret);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。