如何关闭java程序?

时间:2018-12-26 07:12:28

标签: java arrays

无法关闭程序,

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ArrayExample2 {

    public static void main(String[] args) {  
        BufferedReader reader;
            try {
                reader = new BufferedReader(new FileReader("Customer.txt"));
                while (reader != null) {
                    String line = reader.readLine();
                    while (line != null) {
                        String[] lineSplitted = line.split(",");
                        for (int i = 0; i < 3; ++i) {
                            System.out.println("Element at index " + i +": " + lineSplitted[i]);
                        }
                        line = null;
                        System.out.println(lineSplitted[0]);
                        System.out.println(lineSplitted[1]);
                        System.out.println(lineSplitted[2]);
                    } 
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            } 
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您输入的错误

while (reader != null)

因为这是一个无休止的循环

我不知道你为什么写这行代码,但是我认为这不是你想要和需要的:)

哦,尝试使用try-with-resources重写代码,例如:

try (FileReader fr = new FileReader("Customer.txt");
     BufferedReader reader = new BufferedReader(fr)) {
    // your code logic            
    }
} catch (IOException e) {
    System.err.println("IOException while reading file");
    throw new UncheckedIOException(e);
}