读取文件的方式在Java7
在Java7使用Nio对文件的操作变得更加容易了使用
java.nio.file.Files 可以大大的减少代码量,而且在使用过程不需要在finally 进行关闭流的操作了。 这里就使用
readAllBytes(Paths),readAllLines(Paths path,Chatset sc)
大家体会下
public static void main(String[] args) {
try {
Path fp=Paths.get("README.md");
byte[] contentBytes=Files.readAllBytes(fp);
System.out.print(new String(contentBytes));
List lines=Files.readAllLines(Paths.get("README.md"), StandardCharsets.UTF_8);
for (String string : lines) {
System.out.println(string);
}
FileStore fs=Files.getFileStore(Paths.get("F:/"));
System.out.println("得到盘符对应的名称: "+fs.name());
System.out.println("文件的所有者:"+Files.getOwner(fp, LinkOption.values()));
} catch (IOException e) {
e.printStackTrace();
}
}
Reproduced please indicate the author and the source, and error a link to this page.
text link:
//xiaochun.zrlog.com/179.html