java—try、catch和finally for scanner和bufferedreader的合适方法

uurv41yg  于 2021-07-08  发布在  Java
关注(0)|答案(3)|浏览(502)

在我的main方法中,尝试了解扫描器和bufferedreader的try-catch的最佳和最有效的方法。是应该有两个不同的渔获量,还是将它们合并成一个渔获量?

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();
    }
cyej8jka

cyej8jka1#

你不需要两者都用 BufferedReader 以及 Scanner . 1 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;
    }
}
ss2ws0br

ss2ws0br2#

一个挡块就足够了。您可以添加finally块来关闭reader和scanner示例(如果它们不为null)。这样,无论try块是正常完成还是抛出异常,它们都将被关闭。应添加空检查,因为try块可能无法到达两个对象的初始化,因为可能会发生异常。

c9qzyr3d

c9qzyr3d3#

宣布 Scanner scan = null 以及 BufferedReader reader = null; 在try-catch-finally代码块外部,并在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();
    }
}

相关问题