【Java】File类

public class FileDemo {
	private static final long LEVEL = 20150701;//本码讲版本
	/**====================File====================
	 * File 抽象文件系统 通过File可以: 
	 * 1、获取文件或目录的属性信息 
	 * 2、操作文件或目录(创建,删除) 不能访问文件数据
	 ==============================================================*/
	// @Test
	public void testFile1() {
		/*
		 * 获取文件或目录的属性信息 
		 */
		// File.separator层级分隔符,因为不同系统有不同的,所以用这个最通用
		File file = new File("." + File.separator + "demo.txt");
		System.out.println("文件是否存在:" + file.exists());
		System.out.println("获取文件名:" + file.getName());
		System.out.println("获取绝对路径:" + file.getAbsolutePath());
		System.out.println("文件大小:" + file.length() + "byte");
		System.out.println("当前是否为文件:" + file.isFile() + " 是否为目录:"
				+ file.isDirectory());
		System.out.println("最后修改时间:"
				+ new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date(
						file.lastModified())));
	}

	/**
	 * 创建文件
	 */
	// @Test
	public void testCreateFile() throws IOException {
		// 如果是在当前目录下,./ 是可以不写的
		File file = new File("test.txt");
		//boolean exists() 该方法用于判断当前file表示的文件或目录是否在硬盘上对应的位置真实存在 若存在返回true
		if (!file.exists()) {
			// 不存在则创建文件
			file.createNewFile();
			System.out.println("创建完毕");
		}
	}

	/**
	 * 删除文件
	 */
	// @Test
	public void testDelete() {
		File file = new File("test.txt");
		if (file.exists()) {
			file.delete();
			System.out.println("删除完毕");
		}
	}

	/**
	 * 创建及删除目录
	 */
	// @Test
	public void testDir() {
		File dirs = new File("demo");
		if (!dirs.exists()) {
			dirs.mkdir();
			System.out.println("创建完毕");
		} else {
			dirs.delete();
			System.out.println("删除完毕");
		}

	}

	/**
	 * 创建及删除多级目录
	 *  由于dirs.delete()方法只会删除空目录
	 *  对于目录中有文件的情况需单独编程
	 */
	@Test
	public void testDirs() {
		File dirs = new File("a" + File.separator + "b" + File.separator + "c"
				+ File.separator + "d" + File.separator + "e" + File.separator
				+ "f");
		if (!dirs.exists()) {
			dirs.mkdirs();// 区别在于会连同父目录一同创建
			System.out.println("创建完毕");
		} else {
			deleteFiles(new File("a"));
			System.out.println("删除完毕");
		}
	}

	private void deleteFiles(File file) {
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File subs : files) {
				deleteFiles(subs);
			}
		}
		file.delete();
	}

	/**
	 * 获取目录中的所有子项
	 */
	// @Test
	public void testGetFile() {
		File file = new File(".");// .表示当前目录
		File[] subs = file.listFiles(// 获取当前File表示的目录中的所有子项 无参的返回全部
				// 使用文件过滤器 获取当前目录下所有名字以.开头的子项
				new FileFilter() {
					@Override
					public boolean accept(File pathname) {
						return pathname.getName().startsWith(".");
					}
				});
		for (File sub : subs) {
			System.out.println((sub.isDirectory() ? "目录:" : "文件:")
					+ sub.getName());
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xerophyte000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值