File类常用方法介绍

Java文件File类

File类

1 概念

​ java.io.File类:

​ 文件和目录路径名的抽象表示(代表硬盘上的一个文件或者文件夹)

​ 主要用于文件和目录的创建、查找和删除等操作。

1.1 相对路径与绝对路径

​ 相对路径:相对于项目目录的路径,这是一个便捷的路径,开发中经常使用。

​ 绝对路径:从盘符开始的完整路径

1.2 路径的表示方式

​ java中文件路径的表示方式

​ Windows中表示: c:\suns.txt

​ Java中表示: c:\\suns.txt

​ c:/sun.txt

2 构造方法

在这里插入图片描述

	public static void main(String[] args) {
		//方式1:根据路径名创建File对象
		File f=new File("D:/Practice/day01/lina.text");
		//方式2:
		String parent="d:/Practice/day01/";//父路径
		String child="lina.text";//子路径
		File f2=new File(parent, child);
		//方式3:
		File parent1=new File("d:/Practice/day01/");//对象类型
		File f3=new File(parent1, child);
		System.out.println(f);
		System.out.println(f2);
		System.out.println(f3);
	}
3 常用方法
3.1创建功能的方法

在这里插入图片描述
在这里插入图片描述

	public static void main(String[] args) {
		File f=new File("d:/Practice/day01/lina.txt");//不带后缀名也是空文档
		File f2=new File("d:/Practice/day01/myfile/aa");
		//创建目录(文件夹)
		boolean res1=f2.mkdir();//创建单极的目录,已经存在创建失败
		System.out.println(res1);
		res1=f2.mkdirs();//创建多级的目录,已经存在创建失败
		System.out.println(res1);
		try {
			//创建空文件:当前仅当当该文件不存在的时候创建成功,如果 已经存在创建失败。
			boolean res=f.createNewFile();
			System.out.println("创建file " +res);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
3.2删除功能的方法

在这里插入图片描述

	public static void main(String[] args) {
		File f = new File("d:/lina.txt");
		File f2 = new File("D:\\myfile2\\music\\china");
		// 创建目录(文件夹)
		//boolean res = f2.mkdir();//创建单级目录
		boolean res = f2.mkdirs();//创建多级目录
		res=f2.delete();
		System.out.println("删除f2:"+res);
		//删除文件或者目录:delete :只能删除存在的文件或(非空)目录
		res=f.delete();
		System.out.println("删除文件:"+res);
		System.out.println(res);
}
3.3判定功能的方法

在这里插入图片描述
在这里插入图片描述

	public static void validate(File file) {
		boolean res=file.isDirectory();//判定创建是否是一个目录
		boolean res1=file.isFile();//判定是否是一个文件
		if(!res1) {
			System.out.println("file是一个目录");
		}else {
			System.out.println("file是一个文件");
		}
	}
	public static void main(String[] args) {
		File f= new File("d:/Practice/day01/lina.txt");
		File f2=new File("d:/Practice/day01/myfile/aa");
		//判定文件或者目录是否存在
		boolean res =f.exists();
		System.out.println("f是否存在:"+res);
		validate(f);
		res =f2.exists();
		System.out.println("f是否存在:"+res);
		validate(f2);
	}
3.4获取功能的方法

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

	public static void fun(File file) {
		//获取该文件的名称
		String name=file.getName();
		System.out.println("文件名称:"+name);
		//获取文件的路径
		String path=file.getAbsolutePath();
		System.out.println("文件的路径是:"+path);
		//获取文件的长度:如果file是文件,长度就是指文件的大小(字节) 如果file是个目录,返回值就不确定了
		long len=file.length();
		if(file.isFile()) {//file是文件,len获取的就是文件的大小
			System.out.println("文件的长度是:"+len);
		}else {
			System.out.println("目录的长度是返回值不确定:"+len);
		}
	}
	public static void main(String[] args) {
		File file=new File("lina.text");//相对路径
		//文件是否存在
		if(!file.exists()) {
			if(file.isFile()) {
				//创建文件
				try {
					boolean res=file.createNewFile();//创建成功后文件会在项目的根路径下
					if(res) {
						System.out.println("新建文件成功!");
					}else {
						System.out.println("创建文件失败!");
					}
				} catch (IOException e) {
					
					e.printStackTrace();
				}
			}else {
				//创建目录
				boolean res=file.mkdirs();
				if(res) {
					System.out.println("新建目录成功!");
				}else {
					System.out.println("创建目录失败!");
				}
			}
		}
		//获取文件信息
		fun(file);
	}
3.5目录的遍历

在这里插入图片描述
在这里插入图片描述

	public static void fun(File file) {
    	// 获取该目录下的所有的文件名称
		String[] arr=file.list();
        File[] arr2=file.listFiles();
		if(arr!=null) {
			for (String string : arr) {
				System.out.println(string);
			}
		}else {
			System.out.println("不是目录或者目录中没有内容");
		}
	}
	public static void main(String[] args) {
		File file = new File("d:/");
		System.out.println("输出"+file.getName()+"目录下的所有文件内容:");
		fun(file);
	}
4 综合练习

​ 遍历目录中的所有文件

	public static void main(String[] args) {
		File file = new File("C:/Program Files");
		pringFileName(file);
	}
	public static void pringFileName(File file) {
		if (file != null) {
			// 获取指定目录中的所有的File
			File[] fs = file.listFiles();
			if (fs != null) {
				for (File f : fs) {
					if (f != null && f.isFile()) {// 是文件的时候直接打印文件名称
						System.out.println(f.getName());
					} else {// 是文件夹(目录)---继续打印目录中的内容
						pringFileName(f);
					}
				}
			}
		}
	}

​ File类中的方法综合练习

	public static void fun(File file) throws IOException {
		// 判定文件是否存在
		if (!file.exists()) {
			// 判定创建的是目录还是文件夹
			if (file.isDirectory()) {// 是否是一个目录 file.isFile()
				// 是目录--调用目录的创建方法
				boolean res = file.mkdirs();
				if (res) {
					System.out.println("创建" + file.getName() + "目录成功!绝对路径是:" + file.getAbsolutePath());
				} else {
					System.out.println("创建失败!");
				}
			} else {
				// 是文件--调用创建文件的方法
				boolean res = file.createNewFile();
				if (res) {
					System.out.println("创建" + file.getName() + "文件成功!绝对路径是:" + file.getAbsolutePath());
				} else {
					System.out.println("创建失败!");
				}
			}
		}else {
			System.out.println("你创建的文件或目录存在!!");
		}
	}
	public static void main(String[] args) {
		File file = new File("lina.txt");
		try {
			fun(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值