[Java 개발의 길] (20)try-with-resource 예외 선언
59319 단어 java 개발의 길
java7 이전에 프로그램에서 사용한 자원은 명확하게 닫혀야 하기 때문에 과정이 좀 번거롭다. 아래와 같다.
package com.qunar.lectures.tryResource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by xiaosi on 16-3-4.
*/
public class TryResourceDemo {
//
public static List<String> readLines(String resourcePath) {
String path = TryResourceDemo.class.getResource(resourcePath).getPath();
File file = new File(path);
if (!file.exists()) {
throw new RuntimeException("Can not find file + " + resourcePath);
}//if
if (!file.isFile()) {
throw new RuntimeException(resourcePath + " is not a regular file");
}//if
FileInputStream fis;
InputStreamReader isr;
BufferedReader br = null;
try {
fis = new FileInputStream(file);
isr = new InputStreamReader(fis, "UTF-8");
br = new BufferedReader(isr);
List<String> lines = new ArrayList<String>();
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}//while
return lines;
}
catch (IOException e) {
throw new RuntimeException("Read file failed , file=" + resourcePath, e);
}
finally {
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}//if
}//finally
}
public static void main(String[] args) {
List<String> lines = readLines("/a.txt");
for(String line : lines){
System.out.println("line:" + line);
}//for
}
}
try , finally 。 finally 。 ? try , finally 。
private static void printFileJava7() throws IOException {
try(FileInputStream input = new FileInputStream("file.txt")) {
int data = input.read();
while(data != -1){
System.out.print((char) data);
data = input.read();
}
}
}
:
try(FileInputStream input = new FileInputStream("file.txt")) {
try-with-resource 。FileInputStream try 。 , 。 try ,FileInputStream 。 FileInputStream java java.lang.AutoCloseable 。 try-with-resources 。
try-with-resources , FileInputStreami ( close ) ,try-with-resources , FileInputStreami 。 ( finally ) 。
JDK7 AutoCloseable Closeable , try-with-resource 。
:
package com.qunar.lectures.tryResource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by xiaosi on 16-3-4.
*/
public class TryResourceDemo {
//
public static List<String> readLines(String resourcePath) {
String path = TryResourceDemo.class.getResource(resourcePath).getPath();
File file = new File(path);
if (!file.exists()) {
throw new RuntimeException("Can not find file + " + resourcePath);
}//if
if (!file.isFile()) {
throw new RuntimeException(resourcePath + " is not a regular file");
}//if
// try-with-resource
try (FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr)){
List<String> lines = new ArrayList<String>();
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}//while
return lines;
}
catch (IOException e) {
throw new RuntimeException("Read file failed , file=" + resourcePath, e);
}
}
public static void main(String[] args) {
List<String> lines = readLines("/a.txt");
for(String line : lines){
System.out.println("line:" + line);
}//for
}
}