文章目录
JavaIO----File篇
1、初步认识IO(简单用法)
1、如何获取到文件对象
用File 类实现对象, 这个对象就可以指定到具体的 目录 或者 文件。
项目目录结构:
代码: 用几种方式打开同一个test文件夹 (打开文件方式一样,最后指定到文件名就行)
public static void main(String[] args) {
//绝对路经打开文件
File file1 = new File("D:\\CodeFiles\\IOStudy\\test");
//相对路径打开文件, 相对的是项目的项目根目录
File file2 = new File("test");
//父目录 + 父目录下文件 形式参数
File file3 = new File("D:\\CodeFiles\\IOStudy", "test");
File file4 = new File("D:\\CodeFiles\\IOStudy");
//父目录File对象 + 父目录下文件
File file5 = new File(file4, "test");
System.out.println("打开文件");
System.out.println("file1 的 绝对路径" + file1.getAbsolutePath());
System.out.println("file2 的 绝对路径" + file2.getAbsolutePath());
System.out.println("file3 的 绝对路径" + file3.getAbsolutePath());
System.out.println("file4 的 绝对路径" + file4.getAbsolutePath());
System.out.println("file5 的 绝对路径" + file5.getAbsolutePath());
}
注意:双斜杠(\)是因为要给单斜杠(\)一个转义的作用。
文件的名字要用String 类型, 双引号不要忘记。
控制台输出结果:
打开文件
file1 的 绝对路径D:\CodeFiles\IOStudy\test
file2 的 绝对路径D:\CodeFiles\IOStudy\test
file3 的 绝对路径D:\CodeFiles\IOStudy\test
file4 的 绝对路径D:\CodeFiles\IOStudy
file5 的 绝对路径D:\CodeFiles\IOStudy\test
Process finished with exit code 0
2、File对象的一些操作函数:
文件结构:
代码:
public static void main(String[] args) throws IOException {
//打开test文件夹
File file = new File("test\\a.txt");
//1、获取文件的绝对路径 getAbsolutePath
System.out.println("1:");
System.out.println("getAbsolutePath: " );
System.out.println( file.getAbsolutePath());
//2、获取文件夹 或者 文件 的 名字 getName
System.out.println("2:");
System.out.println("getName:" );
System.out.println(file.getName());
//3、获取父目录名字, 就是上一个目录 getParentFile
System.out.println("3:");
System.out.println("getParentFile: ");
System.out.println( file.getParentFile());
//4、获得相对路径
System.out.println("4:");
System.out.println("getPath:");
System.out.println(file.getPath());
//5、判断是不是文件, 是 true ,不是 false
System.out.println("5:");
System.out.println("isFile:");
System.out.println(file.isFile());
//6、判断是不是文件夹 是 true , 否 false
System.out.println("6:");
System.out.println("isDirectory:");
System.out.println(file.isDirectory());
//7、判断当前路径或者文件夹是不是存在, 是 true ,否 false
System.out.println("7:");
System.out.println("exists:");
System.out.println(file.exists());
//8、删除文件 成功true ,失败 false
System.out.println("8:");
System.out.println("delete:");
System.out.println(file.delete());
File dir = new File("test\\b.txt");
//9、在目录下创建问件 会抛出异常
System.out.println("9:");
System.out.println("createNewFile");
System.out.println(dir.createNewFile());
}
控制台结果:
1:
getAbsolutePath:
D:\CodeFiles\IOStudy\test\a.txt
2:
getName:
a.txt
3:
getParentFile:
test
4:
getPath:
test\a.txt
5:
isFile:
true
6:
isDirectory:
false
7:
exists:
true
8:
delete:
true
9:
createNewFile
true
执行之后的目录结构, 删除了a.txt, 新创建了一个b.txt
两个重要方法 length() , listFile();
文件目录
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SzXGKnWj-1667355219381)(C:\Users\86182\AppData\Roaming\Typora\typora-user-images\image-20221102084112997.png)]
其中b.txt文件中存储了 5个字符 12345
代码:
public static void main(String[] args) {
File file = new File("test\\b.txt");
//获取指定文件的大小, 单位是字节
System.out.println(file.length());
File dir = new File("test");
//获取指定路径下的所有文件和目录
File [] files = dir.listFiles();
if(files != null) { //有可能获取失败, files=null, 防止空指针异常
for (File file1 : files) {
System.out.println(file1);
}
}
}
控制台结果
5
test\b.txt
test\c.txt
test\这是文件夹
3、File操作实战----获取指定 目录 或 文件 的大小
代码
//获取文件大小 ,使用的时候直接调用这个函数,异常是获取某个文件的大小失败了, 文件路径 会存在异常对象的message里
public static String getFileLength(File file) throws getLengthException {
long length = getLength(file);
String lengthAndUnit = getLengthAndUnit(length);
return lengthAndUnit;
}
//换算成最大的单位 ,这里支持到PB, 如果要扩大, 可以在数组里面继续添加
public static String getLengthAndUnit(long length){
String [] unit = new String[]{"B", "KB", "MB", "GB", "TB", "PB"};
int flag = 0;
while(length>=1024 && flag<unit.length){
length /= 1024;
flag +=1;
}
return length+unit[flag];
}
//获取指定文件或目录的长度 , 单位是字节, B
public static long getLength(File file) throws getLengthException {
//如果文件不存在 , 返回-1
if(!file.exists()){
return -1L;
}
//如果是文件, 直接返回大小就可以
if(file.isFile()){
return file.length();
}else{
//获取当前目录下的所有文件或者目录
File[] files = file.listFiles();
//非空判断, 防止空指针异常
if(files!=null){
long ans = 0L;
//递归调用, 获得每一个目录或文件的大小, 求和, 返回
for (File file1 : files) {
long temp = getLength(file1);
//temp =-1时, 代表获取大小失败
if(temp == -1L){
throw new getLengthException("获取文件或目录 " + file1.getPath() +" 失败");
}else{
ans += temp;
}
}
return ans;
}else{
//其他情况
return -1L;
}
}
}
}
//异常类, 有可能获取文件大小会失败
class getLengthException extends Exception{
// 空参构造方法
public getLengthException() {
}
//带异常信息的构造方法
/*
查看源码发现,所有的异常类都会有一个带异常信息的构造方法,
方法内部会调用父类带异常信息的构造方法,让父类来处理这个异常信息
*/
public getLengthException(String message){
super(message);
}
}
4、File实战----删除指定目录的文件
代码
//带有删除提示信息, 返回的数组里面会详细记录删除是否成功, 直接调用这个
public static List<String> deleteFileMessage(File file){
List<String> message = new ArrayList<>();
deleteFile(file, message);
if(message.size()==0){
message.add("删除成功!!!");
}
return message;
}
//删除文件或者目录, 实际操作删除的函数
public static void deleteFile(File file, List<String> message) {
//首先判断存不存在
if(!file.exists()){
message.add("文件或路径" + file.getPath() +"不存在,这一文件或路径删除失败");
return ;
}
//如果是文件直接删除
if(file.isFile()){
boolean delete = file.delete();
if(!delete){
message.add("文件" + file.getPath() + "删除失败");
return ;
}
}else{ //处理文件夹
//获取所有目录和文件
File[] files = file.listFiles();
//非空判断
if(files!=null){
for (File file1 : files) {
deleteFile(file1, message);
}
file.delete();
}else {
message.add("文件" + file.getPath() + "删除失败");
return;
}
return ;
}
}