Java IO

2021-02-11 01:16

阅读:427

标签:lan   ota   返回   efi   指定   文件   com   file   path   

结构

创建File

		File file = new File("a.txt");// 默认项目路径
		file.createNewFile();
		file = new File("aaa");
		file.mkdir();
		file = new File("aaa\\bbb\\ccc");
		file.mkdirs();// 创建多级目录
		file.delete();

File 判断功能

		File file = new File("a.txt");
		System.out.println("Create:" + file.createNewFile());
		System.out.println("exists():" + file.exists());// file对象是否存在
		System.out.println("ifFile():" + file.isFile());// 是否文件
		System.out.println("isDirectory():" + file.isDirectory());// 是否文件夹
		System.out.println("isAbsolute():" + file.isAbsolute());// 是否绝对路径
		System.out.println("canRead():" + file.canRead());// 可读
		System.out.println("canWrite():" + file.canWrite());
		System.out.println("isHidden():" + file.isHidden());// 是否隐藏
		System.out.println("delete():" + file.delete());

技术图片

File获取属性

		File file2 = new File("hello\\txt");
		File file = new File(file2, "a.txt");
		System.out.println("mkdris():  " + file2.mkdirs());
		System.out.println("createNewFile():  " + file.createNewFile());
		System.out.println("getName():  " + file.getName());
		System.out.println("getAbsolutePath():  " + file.getAbsolutePath());// 输出绝对路径
		System.out.println("getPath:  " + file.getPath());// 相对路径
		System.out.println("length():  " + file.length());// 文件大小,字节单位
		System.out.println("lastModified():  " + file.lastModified());// 上次修改时间毫秒值
		System.out.println("delete file and rec:  " + file.delete() + "   " + file2.delete());

技术图片

遍历文件夹

                // 遍历盘符
		File[] rootArray = File.listRoots();
		for (File e : rootArray)
			System.out.println(e);

                // 返回指定目录下文件和文件夹名称数组
		File file = new File("D:\\");
		String[] list = file.list();
		for (String e : list)
			System.out.println(e);

                // 返回指定目录下文件和文件夹对象数组
		File[] fList = file.listFiles();
		for (File e : fList)
			System.out.println(e.getName());

判断指定目录下指定后缀文件

		File file = new File("D:\\");
		String[] list = file.list(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				File fTemp = new File(file, name);
				boolean flag = fTemp.isFile();// 判断是否是文件
				boolean flag2 = name.endsWith(".dll");// 判断是否指定后缀
				return flag2 && flag;
			}
		});
		for (String e : list)
			System.out.println(e);

Java IO

标签:lan   ota   返回   efi   指定   文件   com   file   path   

原文地址:https://www.cnblogs.com/xiongyungang/p/12740355.html


评论


亲,登录后才可以留言!