[size=16.0000pt](一)File类 用于封装文件和文件夹的类 getName(); //名称 getAbsolutePath(); //绝对路径 getPath(); //相对路径 lastmodefied(); //修改时间 createNewFile(); //创建 delete(); //删除 mkdir();mkdirs(); //创建目录 exists(); //文件是否存在 isFile();isDirectory();isHidden(); renameTo(File); //重命名 listRoots(); //文件系统根 getFreeSpace(); //未使用空间 getTotalSpace();//总容量 getUsableSpace();//未使用空间 list(); //返回目录下的所有文件名称列表 list(FilenameFilter); //文件名称过滤器 listFiles(); //返回目录下的所有文件列表 listFiles(FileFilter); //文件过滤器 listFiles(FilenameFilter); //文件名称过滤器 [size=16.0000pt](二)深度遍历深度遍历文件夹----递归 isDirectory(); listFiles(); public static void lookFileAll(File dir,String kong){ //System.out.println(kong+"|--"+dir.getAbsolutePath()); kong += " "; File[] files = dir.listFiles(new Filter()); if(files!=null) for(File file: files){ //目录 if(file.isDirectory()){ lookFileAll(file,kong); } //文件 else{ System.out.println(kong+"|--"+file.getAbsolutePath()); } } } //过滤器 class Filter implements FileFilter{ @Override public boolean accept(File pathname) { return pathname.isDirectory()?true:pathname.getName().endsWith(".avi"); } } [size=16.0000pt](三)Properties集合 键值都是字符串,一般用于操作键值形式的配置文件 setProperty(key,value); getProperty(key); stringPropertyNames(); list(PrintString);list(PrintWriter); // 将键值输出 load(InputStream);load(Reader); //从文件中加载到内存 store(OutputStream);store(Writer); //将内存持久化到文件 [size=16.0000pt](四)练习:判断程序运行次数判断应用程序运行次数,超过5次,就提示注册。 /** 练习1:判断应用程序运行次数,超过5次,就提示注册 * 1. 计数器 * 2. 操作后将操作次数持久化 * 3. 每次操作都加载持久化文件 */ public static boolean isrun() throws IOException{ boolean flug = true; Properties pro = new Properties(); File file = new File("pro.properties"); if(!file.exists()){ file.createNewFile(); } pro.load(new FileReader(file)); String str = pro.getProperty("num"); int num = 0; if(str != null){ num = Integer.parseInt(str); } if(++num >=5){ flug = false; } pro.setProperty("num", num+""); pro.store(new FileWriter(file), "zj is love"); return flug; }
|