Scanner 和 BufferedReader 的 try、catch 和 finally 的正确方法
Proper method for a try, catch, and finally for Scanner and BufferedReader
试图了解在我的主要方法中对 Scanner 和 BufferedReader 进行 try-catch-finally 的最佳和最有效的方法。应该有两个不同的捕获物还是将它们合并为一个?
public static void main(String[] args) throws IOException {
//open file input stream
try {
BufferedReader reader = new BufferedReader(new FileReader("DATABASE.txt"));
//read file line by line
String line = null;
Scanner scan = null;
BST tree = new BST();
line = reader.readLine();
while(line != null) {
Employee emp = new Employee();
scan = new Scanner(line);
scan.useDelimiter(",");
while(scan.hasNext()){
emp.setEmployeeID(Integer.parseInt(scan.next()));
emp.setFirstName(scan.next());
emp.setLastName(scan.next());
emp.setSalary(Double.parseDouble(scan.next()));
line = reader.readLine();
}
tree.insert(emp);
}
reader.close();
tree.inOrder();
//System.out.println(empList);
} catch(IOException e){
e.printStackTrace();
}
单个 catch 块应该足够了。您可以添加一个 finally 块来关闭 reader 和扫描仪实例(如果它们不为空)。这样,无论 try 块是否正常完成或抛出异常,它们都将被关闭。应该添加空检查,因为 try 块可能无法到达两个对象的初始化,因为可能会发生异常。
在 try-catch-finally 代码块外声明了 Scanner scan = null
和 BufferedReader reader = null;
并在 try 块内初始化了它们。异常被捕获,他们都到达了 finally 块。
public static void main(String[] args) throws IOException {
Scanner scan = null;
BufferedReader reader = null;
//open file input stream
try {
reader = new BufferedReader(new FileReader("DATABASE.txt"));
//read file line by line
String line = null;
Scanner scan = null;
BST tree = new BST();
line = reader.readLine();
scan = new Scanner(line);
while (line != null) {
Employee emp = new Employee();
scan.useDelimiter(",");
while (scan.hasNext()) {
emp.setEmployeeID(Integer.parseInt(scan.next()));
emp.setFirstName(scan.next());
emp.setLastName(scan.next());
emp.setSalary(Double.parseDouble(scan.next()));
line = reader.readLine();
}
tree.insert(emp);
}
}catch (IOException e) {
e.printStackTrace();
} finally {
reader.close();
scan.close();
}
}
您不需要同时使用 BufferedReader
和 Scanner
。一个Scanner
就够了:
public static BST readFile(File file) throws FileNotFoundException {
try (Scanner scan = new Scanner(file)) {
scan.useLocale(Locale.US); // mandatory for read double values
BST bst = new BST();
while (scan.hasNext()) {
Employee employee = new Employee();
employee.setEmployeeID(scan.nextInt());
employee.setFirstName(scan.next());
employee.setLastName(scan.next());
employee.setSalary(scan.nextDouble());
bst.insert(employee);
}
bst.inOrder();
return bst;
}
}
试图了解在我的主要方法中对 Scanner 和 BufferedReader 进行 try-catch-finally 的最佳和最有效的方法。应该有两个不同的捕获物还是将它们合并为一个?
public static void main(String[] args) throws IOException {
//open file input stream
try {
BufferedReader reader = new BufferedReader(new FileReader("DATABASE.txt"));
//read file line by line
String line = null;
Scanner scan = null;
BST tree = new BST();
line = reader.readLine();
while(line != null) {
Employee emp = new Employee();
scan = new Scanner(line);
scan.useDelimiter(",");
while(scan.hasNext()){
emp.setEmployeeID(Integer.parseInt(scan.next()));
emp.setFirstName(scan.next());
emp.setLastName(scan.next());
emp.setSalary(Double.parseDouble(scan.next()));
line = reader.readLine();
}
tree.insert(emp);
}
reader.close();
tree.inOrder();
//System.out.println(empList);
} catch(IOException e){
e.printStackTrace();
}
单个 catch 块应该足够了。您可以添加一个 finally 块来关闭 reader 和扫描仪实例(如果它们不为空)。这样,无论 try 块是否正常完成或抛出异常,它们都将被关闭。应该添加空检查,因为 try 块可能无法到达两个对象的初始化,因为可能会发生异常。
在 try-catch-finally 代码块外声明了 Scanner scan = null
和 BufferedReader reader = null;
并在 try 块内初始化了它们。异常被捕获,他们都到达了 finally 块。
public static void main(String[] args) throws IOException {
Scanner scan = null;
BufferedReader reader = null;
//open file input stream
try {
reader = new BufferedReader(new FileReader("DATABASE.txt"));
//read file line by line
String line = null;
Scanner scan = null;
BST tree = new BST();
line = reader.readLine();
scan = new Scanner(line);
while (line != null) {
Employee emp = new Employee();
scan.useDelimiter(",");
while (scan.hasNext()) {
emp.setEmployeeID(Integer.parseInt(scan.next()));
emp.setFirstName(scan.next());
emp.setLastName(scan.next());
emp.setSalary(Double.parseDouble(scan.next()));
line = reader.readLine();
}
tree.insert(emp);
}
}catch (IOException e) {
e.printStackTrace();
} finally {
reader.close();
scan.close();
}
}
您不需要同时使用 BufferedReader
和 Scanner
。一个Scanner
就够了:
public static BST readFile(File file) throws FileNotFoundException {
try (Scanner scan = new Scanner(file)) {
scan.useLocale(Locale.US); // mandatory for read double values
BST bst = new BST();
while (scan.hasNext()) {
Employee employee = new Employee();
employee.setEmployeeID(scan.nextInt());
employee.setFirstName(scan.next());
employee.setLastName(scan.next());
employee.setSalary(scan.nextDouble());
bst.insert(employee);
}
bst.inOrder();
return bst;
}
}