本文共 911 字,大约阅读时间需要 3 分钟。
Path使用
构造Path对象
Path path1 = Paths.get("C:/", "Xmp");Path path2 = Paths.get("C:/Xmp");
File和Path之间的转换
File file = new File("C:/my.ini");Path p1 = file.toPath();File file = p1.toFile();
创建文件
Path p = Paths.get("C:\\mystuff.txt");if (!Files.exists(p)) { try { Files.createFile(p); } catch (IOException e) { e.printStackTrace(); }}
读取文件
Path p = Paths.get("C:\\mystuff.txt");try { BufferedReader reader = Files.newBufferedReader(p); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); }} catch (IOException e) { e.printStackTrace();}
写入文件
Path p = Paths.get("C:\\mystuff.txt");try { BufferedWriter writer = Files.newBufferedWriter(p); writer.write("hello"); writer.write("world"); writer.flush(); writer.close();} catch (IOException e) { e.printStackTrace();}
转载于:https://www.cnblogs.com/liuweiqc/p/10984589.html