分裂后阅读内容

时间:2015-06-20 03:58:40

标签: java split

我的问题是我有一个代码而且我有修复"无法找到符号的问题"错误。这是代码。

public static void writer() throws IOException {
    FileReader in = null;
    FileWriter out = null;
    BufferedReader br = null;
    BufferedWriter bw = null;
    try {
        in = new FileReader("Student.txt");
        out = new FileWriter("StudentAvg.txt");
        br = new BufferedReader(in);
        bw = new BufferedWriter(out);
        String[] line = new String[28];
        line = in.split("\t");

错误指向拆分过程。我的变量有问题吗?

1 个答案:

答案 0 :(得分:2)

问题是,你还没有读过任何一行。

br = new BufferedReader(in);
bw = new BufferedWriter(out);
String[] line = new String[28];
line = in.split("\t"); // <-- this is your input file reader.

我想你想要

br = new BufferedReader(in);
bw = new BufferedWriter(out);
String fromFile;
while ((fromFile = br.readLine()) != null) {
    String[] line = fromFile.split("\t");

您也可以使用try-with-resources

try (BufferedReader br = new BufferedReader(//
        new FileReader("Student.txt"));
        BufferedWriter bw = new BufferedWriter(//
                new FileWriter("StudentAvg.txt"))) {
    String fromFile;
    while ((fromFile = br.readLine()) != null) {
        String[] line = fromFile.split("\t");
    }
}