当前位置:网站首页>File类的学习过程中出现的问题及解决方法
File类的学习过程中出现的问题及解决方法
2022-07-26 12:02:00 【王小小鸭】
1. 利用File构造器,new 一个文件目录file 1)在其中创建多个文件和目录,测试自己学过的File类的方法 2)手动编写方法,实现删除file中指定文件的操作 2. 判断指定目录下是否有后缀名为.jpg的文件,如果有,就输出该文件名称 3. 遍历指定目录所有文件名称,包括子文件目录中的文件(注意这句话,经典递归方法,不知道思路的直接问)。 4. 创建一个与a.txt文件同目录下的另外一个文件b.txt 5.操作D盘下的my.txt文件 1) 判断my.txt文件是否存在 2) 若存在则删除;若不存在则创建 6.File类的作用是什么?它是一种流吗? 7.使用File类删除某个文件夹(例如D盘下的temp文件夹)下的所有文件和文件夹: 1) 判断temp文件夹下的文件类型,如果是文件则直接删除 2) 如果是文件夹则获取该文件夹下的子文件和文件夹 3) 使用递归的方式删除所有temp文件夹下的文件和文件夹(可参考3题)
1. 利用File构造器,new 一个文件目录file
1)在其中创建多个文件和目录,测试自己学过的File类的方法
2)手动编写方法,实现删除file中指定文件的操作*/
1.1代码:
package com.B.IOStream_14.FileDemo.Ask;
import java.io.File;
import java.io.IOException;
public class FileA1 {
public static void main(String[] args) throws IOException {
//创建对象 、文件
File f1 = new File("E:\\JavaDemo\\Demo03\\Web");
System.out.println(f1.mkdirs()); //创建多级目录
File f2 = new File("E:\\JavaDemo\\Demo04");
System.out.println(f2.mkdir()); //创建单级目录
File f3 = new File("E:\\JavaDemo\\Demo04\\今天周五很开心.txt");
System.out.println(f3.createNewFile()); //创建文件
File f4 = new File("E:\\JavaDemo\\Demo04\\明天周六更开心.txt");
System.out.println(f4.createNewFile()); //创建文件
File f5 = new File("E:\\JavaDemo\\Demo04\\后天周日也开心.txt");
System.out.println(f5.createNewFile()); //创建文件
System.out.println("-----------");
// 删除文件f5
f5.delete();
//根据给定的路径创建一个File对象
File file = new File("E:\\JavaDemo");
//调用方法
textFile(file);
// 输出文件路径
System.out.println(file);
}
// 编写方法,实现删除file中指定文件的操作
public static void textFile(File file) {
// file对象中的文件存储到File文件数组中
File[] fileArrary = file.listFiles();
if (fileArrary != null) { //增加判断语句非空,增加健壮性
for (File f : fileArrary) { //增强for循环遍历集合,第一个for删除内容
//判断该文件是否是文件目录 是递归 否删除
if (f.isDirectory()) {
//是:递归调用
textFile(f);
} else
//不是:获取绝对路径输出在控制台
System.out.println(f.getAbsoluteFile());
}
}
}
}运行如下:
运行时发现未能输出其他文件,debug调试时是有这些内容有出现但并未输出,
创建文件:
删除文件:
判断指定目录下是否有后缀名为.jpg的文件,如果有,就输出该文件名称
代码:
package com.B.IOStream_14.FileDemo.Ask; import java.io.File; import java.io.IOException; //2. 判断指定目录下是否有后缀名为.jpg的文件,如果有,就输出该文件名称 public class FileA2 { public static void main(String[] args) throws IOException { //1.创建一个当前文件所在的路径 File file = new File("E:\\JavaDemo\\Demo04"); File f1 = new File("E:\\JavaDemo\\Demo04\\买了大橘大利娃衣真开心.txt"); System.out.println(f1.createNewFile()); //创建文件 File f2 = new File("E:\\JavaDemo\\Demo04\\没钱了QAQ.txt"); System.out.println(f2.createNewFile()); //创建文件 File f3 = new File("E:\\JavaDemo\\Demo04\\缪拉宝宝.jpg"); System.out.println(f3.createNewFile()); //创建文件 //2.用当前目录下的文件名存放到字符数组当中 String[] fileNames = file.list(); for (String fileName : fileNames) { //Java endsWith() 方法 if (fileName.endsWith(".jpg")) { System.out.println(fileName); } } //endsWith() 方法用于测试字符串是否以指定的后缀结束。 //语法 //public boolean endsWith(String suffix) //参数 //suffix -- 指定的后缀。 //返回值 //如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回 true;否则返回 false。注意,如果参数是空字符串,或者等于此 String 对象(用 equals(Object) 方法确定),则结果为 true。 } }拓展:
引入了JavaendsWith() 方法
endsWith() 方法用于测试字符串是否以指定的后缀结束。
语法
public boolean endsWith(String suffix)
参数
suffix -- 指定的后缀。
如:
for (String fileName : fileNames) { //Java endsWith() 方法 if (fileName.endsWith(".jpg")) { System.out.println(fileName); } }
返回值
如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回 true;否则返回 false。注意,如果参数是空字符串,或者等于此 String 对象(用 equals(Object) 方法确定),则结果为 true。
运行:

遍历指定目录所有文件名称,包括子文件目录中的文件(注意这句话,经典递归方法,不知道思路的直接问)。
代码:
package com.B.IOStream_14.FileDemo.Ask; import java.io.File; import java.io.IOException; //3.遍历指定目录所有文件名称,包括子文件目录中的文件(注意这句话,经典递归方法) public class FileA3 { public static void main(String[] args) throws IOException { //1.创建一个当前文件所在的路径 File file = new File("E:\\JavaDemo"); //调用方法 textFile(file); System.out.println(file); } // 编写方法,实现删除file中指定文件的操作 public static void textFile(File file) { // file对象中的文件存储到File文件数组中 File[] fileArrary = file.listFiles(); if (fileArrary != null) { //增加判断语句非空,增加健壮性 for (File f : fileArrary) { //增强for循环遍历集合,第一个for删除内容 //判断该文件是否是文件目录 是递归 否删除 if (f.isDirectory()) { //是:递归调用 textFile(f); } else //不是:获取绝对路径输出在控制台 System.out.println(f.getAbsoluteFile()); } } } }
运行:

6.File类的作用是什么?它是一种流吗?
File类的作用是实现java与文件数据交互的类,不是流.
7.使用File类删除某个文件夹(例如D盘下的temp文件夹)下的所有文件和文件夹: 1) 判断temp文件夹下的文件类型,如果是文件则直接删除 2) 如果是文件夹则获取该文件夹下的子文件和文件夹 3) 使用递归的方式删除所有temp文件夹下的文件和文件夹(可参考3题)
package com.B.IOStream_14.FileDemo.Ask;
import java.io.File;
import java.io.IOException;
//7.使用File类删除某个文件夹(例如D盘下的temp文件夹)下的所有文件和文件夹:
//1) 判断temp文件夹下的文件类型,如果是文件则直接删除
//2) 如果是文件夹则获取该文件夹下的子文件和文件夹
//3) 使用递归的方式删除所有temp文件夹下的文件和文件夹(可参考3题)
public class FileA7 {
public static void main(String[] args) throws IOException {
//1.创建一个当前文件所在的路径
File file = new File("D:\\temp");
System.out.println(file.mkdir());
// 创建文件
File f3 = new File("D:\\temp\\今天周五很开心.txt");
System.out.println(f3.createNewFile()); //创建文件
File f4 = new File("D:\\temp\\明天周六更开心.txt");
System.out.println(f4.createNewFile()); //创建文件
File f5 = new File("D:\\temp\\后天周日也开心.txt");
System.out.println(f5.createNewFile()); //创建文件
System.out.println("-----------");
//调用方法
textFile(file);
System.out.println(file);
System.out.println("-----------");
file.delete();
System.out.println(file);
}
// 编写方法,实现删除file中指定文件的操作
public static void textFile(File file) {
// file对象中的文件存储到File文件数组中
File[] fileArrary = file.listFiles();
if (fileArrary != null) { //增加判断语句非空,增加健壮性
for (File f : fileArrary) { //增强for循环遍历集合,第一个for删除内容
//判断该文件是否是文件目录 是递归 否删除
if (f.isDirectory()) {
//是:递归调用
textFile(f);
} else
//不是:获取绝对路径输出在控制台
System.out.println(f.getAbsoluteFile());
}
}
}
}创建文件目录temp和文件内容:

执行删除并打印:

边栏推荐
- Several inventory terms often used in communication
- RFID的工作原理
- What is oom, why it happens and some solutions
- 面试官:如何理解QPS,TPS,RT?
- 羽毛球馆的两个基础设施你了解多少?
- 大佬们,cdc oracle 怎么设置从指定scn号开始读取,或是怎么设置只读全量的归档,不去读取快
- [Anhui University] information sharing of postgraduate entrance examination and re examination
- Miccai2022 paper | evolutionary multi-objective architecture search framework: application in covid-19 3D CT classification
- Pytest interface automation test framework | setup and teardown functions of pytest
- 以太网驱动详解之RMII、SMII、GMII、RGMII接口
猜你喜欢

Introduction to FPGA (II) - one out of two selector

pytest接口自动化测试框架 | pytest配置文件

Some practical, commonly used and increasingly efficient kubernetes aliases

4.1 configure MySQL and register login module

干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)

种种迹象表明,Apple将有望支持AV1

Pytorch深度学习快速入门教程 -- 土堆教程笔记(一)

海外APP推送(下篇):海外厂商通道集成指南

Redis database, which can be understood by zero foundation Xiaobai, is easy to learn and use!

二、容器_
随机推荐
Pytoch deep learning quick start tutorial -- mound tutorial notes (II)
大量if else判断如何优化?@Valib详解
pytest接口自动化测试框架 | pytest之fixture介绍
什么是Per-Title编码?
Pytoch deep learning quick start tutorial -- mound tutorial notes (I)
Redis database, which can be understood by zero foundation Xiaobai, is easy to learn and use!
Use the jsonobject object in fastjason to simplify post request parameter passing
大佬们,请教一下,我按照文档配了cdc连接oracle,总是运行报错找不到类 ValidstionE
CVPR 2022 new SOTA for monocular depth estimation new CRFs: neural window fullyconnected CRFs
【Mysql约束】
X.509、PKCS文件格式介绍
按位与怎么写SQL
Network protocol: tcp/ip protocol
Li Kai: the interesting and cutting-edge audio and video industry has always attracted me
二、容器_
RFID的工作原理
Sim900a based on STM32 sends short messages in Chinese and English
面试官:如何处理高并发?
MATLAB中strjoin函数使用
3D point cloud course (VIII) -- feature point matching

