
2.5.2 读写文件
Files类可以使得普通文件操作变得快捷。例如,可以用下面的方式很容易地读取文件的所有内容:

如果想将文件当作字符串读入,那么可以在调用readAllBytes之后执行下面的代码:

但是如果希望将文件当作行序列读入,那么可以调用:

相反地,如果希望写出一个字符串到文件中,可以调用:

向指定文件追加内容,可以调用:

还可以用下面的语句将一个行的集合写出到文件中:

这些简便方法适用于处理中等长度的文本文件,如果要处理的文件长度比较大,或者是二进制文件,那么还是应该使用所熟知的输入/输出流或者读入器/写出器:

这些便捷方法可以将你从处理FileInputStream、FileOutputStream、BufferedReader和BufferedWriter的繁复操作中解脱出来。
java.nio.file.Files 7
·static byte[]readAllBytes(Path path)
·static List<String>readAllLines(Path path,Charset charset)
读入文件的内容。
·static Path write(Path path,byte[]contents,OpenOption...options)
·static Path write(Path path,Iterable<?extends CharSequence>contents,OpenOption options)
将给定内容写出到文件中,并返回path。
·static InputStream newInputStream(Path path,OpenOption...options)
·static OutputStream newOutputStream(Path path,OpenOption...options)
·static BufferedReader newBufferedReader(Path path,Charset charset)
·static BufferedWriter newBufferedWriter(Path path,Charset charset,OpenOption...options)
打开一个文件,用于读入或写出。