目录
前言
主要总结JDK7 中不常用的特性。
try-with-resources
在JDK7中 ,所有的流,都实现了一个接口叫做 AutoCloseable,任何类实现了这个接口,都可以在try()中进行实例化。并且在try, catch, finally结束的时候自动关闭,回收相关资源,所以可以不使用close()
Example
try (FileInputStream fis = new FileInputStream(file)){
fis.read(all);
for (byte b:all){
System.out.println(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
捕获多个Exception
当我们在操作一个对象的时候,有时候它会抛出多个异常,如:
try {
Thread.sleep(20000);
FileInputStream fis = new FileInputStream("/a/b.txt");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
这样代码写起来要捕获很多异常,不是很优雅,JDK7种允许你捕获多个异常:
try {
Thread.sleep(20000);
FileInputStream fis = new FileInputStream("/a/b.txt");
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
处理反射异常
我们有时候操作反射方法的时候会抛出很多不相关的检查异常,例如:
try {
Class<?> clazz = Class.forName("com.biezhi.apple.User");
clazz.getMethods()[0].invoke(object);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
尽管你可以使用catch多个异常的方法将上述异常都捕获,但这也让人感到痛苦。 JDK7修复了这个缺陷,引入了一个新类ReflectiveOperationException可以帮你捕获这些反射异常:
try {
Class<?> clazz = Class.forName("com.biezhi.apple.User");
clazz.getMethods()[0].invoke(object);
} catch (ReflectiveOperationException e){
e.printStackTrace();
}
文件操作
我们知道在JDK6甚至之前的时候,我们想要读取一个文本文件也是非常麻烦的一件事,而现在他们都变得简单了, 这要归功于NIO2,我们先看看之前的做法:
BufferedReader br = null;
try {
new BufferedReader(new FileReader("file.txt"));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
} catch (Exception e){
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
大家对这样的一段代码一定不陌生,但这样太繁琐了,我只想读取一个文本文件,要写这么多代码还要 处理让人头大的一堆异常,怪不得别人吐槽Java臃肿,是在下输了。。
Path
Path用于来表示文件路径和文件,和File对象类似,Path对象并不一定要对应一个实际存在的文件, 它只是一个路径的抽象序列。
要创建一个Path对象有多种方法,首先是final类Paths的两个static方法,如何从一个路径字符串来构造Path对象:
Path path1 = Paths.get("/home/biezhi", "a.txt");
Path path2 = Paths.get("/home/biezhi/a.txt");
URI u = URI.create("file:////home/biezhi/a.txt");
Path pathURI = Paths.get(u);
通过FileSystems构造
Path filePath = FileSystems.getDefault().getPath("/home/biezhi", "a.txt");
Path、URI、File之间的转换
File file = new File("/home/biezhi/a.txt");
Path p1 = file.toPath();
p1.toFile();
file.toURI();
读写文件
你可以使用Files类快速实现文件操作,例如读取文件内容:
byte[] data = Files.readAllBytes(Paths.get("/home/biezhi/a.txt"));
String content = new String(data, StandardCharsets.UTF_8);
如果希望按照行读取文件,可以调用
List<String> lines = Files.readAllLines(Paths.get("/home/biezhi/a.txt"));
反之你想将字符串写入到文件可以调用
Files.write(Paths.get("/home/biezhi/b.txt"), "Hello JDK7!".getBytes());
你也可以按照行写入文件,Files.write方法的参数中支持传递一个实现Iterable接口的类实例。 将内容追加到指定文件可以使用write方法的第三个参数OpenOption:
Files.write(Paths.get("/home/biezhi/b.txt"), "Hello JDK7!".getBytes(),StandardOpenOption.APPEND);
默认情况Files类中的所有方法都会使用UTF-8编码进行操作,当你不愿意这么干的时候可以传递Charset参数进去变更
当然Files还有一些其他的常用方法:
InputStream ins = Files.newInputStream(path);
OutputStream ops = Files.newOutputStream(path);
Reader reader = Files.newBufferedReader(path);
Writer writer = Files.newBufferedWriter(path);
创建、移动、删除
创建文件、目录
if (!Files.exists(path)) {
Files.createFile(path);
Files.createDirectory(path);
}
Files还提供了一些方法让我们创建临时文件/临时目录:
Files.createTempFile(dir, prefix, suffix);
Files.createTempFile(prefix, suffix);
Files.createTempDirectory(dir, prefix);
Files.createTempDirectory(prefix);
这里的dir是一个Path对象,并且字符串prefix和suffix都可能为null。 例如调用Files.createTempFile(null, “.txt”)会返回一个类似/tmp/21238719283331124678.txt
读取一个目录下的文件请使用Files.list和Files.walk方法
复制、移动一个文件内容到某个路径
Files.copy(in, path);
Files.move(path, path);
删除一个文件
Files.delete(path);