Java-IO流之File操作和Properties操作
java的File类主要是用来操作文件的元数据,稍作演示如下:
其中方法getAllJavaFile()是使用了过滤器FileFileter,这个过滤器只需要实现accept方法,判断什么样的文件返回true就行。蛮简单的直接贴代码:
/** * File类主要是用来操作文件的元数据 * @param args * @throws IOException */ public static void main(String[] args) throws IOException { File file = new File("file.txt"); if (file == null) { file.createNewFile(); } System.out.println(file.getName()); System.out.println(file.getParent()); System.out.println(file.getPath()); System.out.println(file.getAbsolutePath()); System.out.println(file.length()); System.out.println(file.getUsableSpace()); System.out.println(file.lastModified()); boolean isDelete = false; if (isDelete = file.delete()) { System.out.println("删除成功!"); } if (file.isDirectory()) { System.out.println("这是一个文件夹"); } if (file.isFile()) { System.out.println("这是一个文件夹"); } if (file.exists()) { System.out.println("该文件存在~"); } System.out.println("遍历文件夹:" + System.getProperty("user.dir")); File rootDir = new File(System.getProperty("user.dir")); getAllFiles(rootDir); FileFilter filter = new FileFilter() { @Override public boolean accept(File pathname) { if (pathname.getName().endsWith("java")) { return true; } return false; } }; System.out.println("========================================================================="); getAllJavaFile(rootDir, filter); System.out.println("========================================================================="); } private static void getAllJavaFile(File rootDir, FileFilter filter) { if (rootDir.isDirectory()) { File[] javaFiles = rootDir.listFiles(filter); for (File item : javaFiles) { System.out.println(item.getName()); } File[] files = rootDir.listFiles(); for (File item : files) { if (item.isDirectory()) { getAllJavaFile(item, filter); } } } else { System.out.println(rootDir.getName()); } } private static void getAllFiles(File rootDir) { if (rootDir.isDirectory()) { File[] files = rootDir.listFiles(); for (File item : files) { if (item.isDirectory()) { getAllFiles(item); } else { System.out.println(item.getName()); } } } else { System.out.println(rootDir.getName()); } }
Properties主要用来存储配置信息,可以从流中进行读取,存储到磁盘中,下面是基本使用:
1、getProperties和setProperties方法用来对获得和设置相应的键值对;
2、list方法用来查看所有的信息,
3、store方法用来将配置信息写入磁盘;
public static void main(String[] args) throws IOException { Properties properties = new Properties(); properties.setProperty("1", "1"); properties.setProperty("2", "2"); properties.setProperty("3", "3"); properties.setProperty("4", "4"); PrintStream print = System.out; properties.list(print); Properties pFromStream = new Properties(); try (FileInputStream in = new FileInputStream(System.getProperty("user.dir") + "\\eke.test.first.common\\src\\main\\resources\\database.properties")) { pFromStream.load(in); // 从流中读取properties信息 } pFromStream.list(print); pFromStream.setProperty("test", "1000"); try (FileOutputStream out = new FileOutputStream(System.getProperty("user.dir") + "\\eke.test.first.common\\src\\main\\resources\\database.properties")) { pFromStream.store(out, "I have nothing to say~"); // 存储到磁盘 } pFromStream.list(print); // 调用list方法,传入输出流参数打印所有的信息
结果如下:
-- listing properties -- 4=4 3=3 2=2 1=1 -- listing properties -- port=3306 password=123456 jdbc.driver=com.mysql.jdbc.Driver database=exe_course server=jdbc:mysql://localhost username=root test=test -- listing properties -- port=3306 password=123456 jdbc.driver=com.mysql.jdbc.Driver database=exe_course server=jdbc:mysql://localhost username=root test=1000