文件中的数字

huangapple go评论70阅读模式
英文:

Numbers in a file

问题

public static void main(String[] args) throws IOException {
    File numbers = new File("C:\\numbers.txt");
    File positiveNumbers = new File("C:\\positive_numbers.txt");
    File negativeNumbers = new File("C:\\negative_numbers.txt");

    try (
        BufferedWriter wr = new BufferedWriter(new FileWriter(numbers));
        BufferedReader rd = new BufferedReader(new FileReader(numbers));
        BufferedWriter brNegative = new BufferedWriter(new FileWriter(negativeNumbers));
        BufferedWriter brPositive = new BufferedWriter(new FileWriter(positiveNumbers));
    ) {
        if (numbers.exists()) {
            for (int i = 1; i <= 100; i++) {
                wr.write(i + " ");
            }
            for (int a = -1; a >= -100; a--) {
                wr.write(a + " ");
            }

            wr.flush();  // Important: flush the buffer to write the changes to the file

            String line = rd.readLine();
            String[] numbersArray = line.split(" ");
            for (String numStr : numbersArray) {
                int num = Integer.parseInt(numStr);
                if (num > 0) {
                    brPositive.write(num + " ");
                } else if (num < 0) {
                    brNegative.write(num + " ");
                }
            }

            brPositive.flush();  // Flush the buffer to write the positive numbers
            brNegative.flush();  // Flush the buffer to write the negative numbers
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
英文:

I'm just a beginner and got the following task:

Write first 100 positive and 100 negative integers to the file, listing them separated by a space.
Then read this file and put the read numbers into 2 files: positive_numbers and negative_numbers.

public static void main(String[] args) throws IOException {
File numbers = new File(&quot;C:\\numbers.txt&quot;);
File positivNumbers = new File(&quot;C:\\positivnumbers.txt&quot;);
File negativNumbers = new File(&quot;C:\\negativnumbers.txt&quot;);
try (
BufferedWriter wr = new BufferedWriter(new FileWriter(numbers));
BufferedReader rd = new BufferedReader(new FileReader(numbers));
BufferedWriter brnegativ = new BufferedWriter(new FileWriter(negativNumbers));
BufferedWriter brpositiv = new BufferedWriter(new FileWriter(positivNumbers));) {
if (numbers.exists()) {
for (int i = 0; i &lt;= 100; i++) {
wr.write(String.valueOf((i) + &quot; &quot;));
}
for (int a = -1; a &gt;= -100; a--) {
wr.write(((a) + &quot; &quot;));
}
String line = rd.readLine();
while (line != null) {
brpositiv.write(line);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

I could write numbers as a String to file "numbers". But I cannot read and write them in "positiv" output file.The file is empty. Where is my mistake?

答案1

得分: 3

在写入操作之后,您需要关闭 wr,这样内存中的所有数据都会刷新到文件中。在关闭之后,您可以重新打开它进行读取。因此,在您的代码中,您打开 rd 太早了。

try (BufferedWriter wr = new BufferedWriter(new FileWriter(numbers));) {
    for (int i = 0; i <= 100; i++) {
        wr.write(String.valueOf((i) + " "));
    }
    for (int a = -1; a >= -100; a--) {
        wr.write(((a) + " "));
    }
} catch (IOException e) {
    e.printStackTrace();
}
// 文件在 try-with-resources 中被关闭...

// ... 所以现在我们可以打开它进行读取:
try (BufferedReader rd = new BufferedReader(new FileReader(numbers));
     BufferedWriter brnegativ = new BufferedWriter(new FileWriter(negativNumbers));
     BufferedWriter brpositiv = new BufferedWriter(new FileWriter(positivNumbers));) {
        String line = rd.readLine();
        while (line != null) {
            brpositiv.write(line);
           // TODO : 分割逻辑
        }
} catch (IOException e) {
    e.printStackTrace();
}
英文:

After the writing you have to close wr so all data in memory gets flushed to the file. AFTER the close you can reopen it for read. So in your code you open rd too soon.

   try (BufferedWriter wr = new BufferedWriter(new FileWriter(numbers));) {
for (int i = 0; i &lt;= 100; i++) {
wr.write(String.valueOf((i) + &quot; &quot;));
}
for (int a = -1; a &gt;= -100; a--) {
wr.write(((a) + &quot; &quot;));
}
} catch (IOException e) {
e.printStackTrace();
}
// file is closed by try-with-resources ...
// ... so now we can open it for read:
try (BufferedReader rd = new BufferedReader(new FileReader(numbers));
BufferedWriter brnegativ = new BufferedWriter(new FileWriter(negativNumbers));
BufferedWriter brpositiv = new BufferedWriter(new FileWriter(positivNumbers));) {
String line = rd.readLine();
while (line != null) {
brpositiv.write(line);
// TODO : split logic
}
} catch (IOException e) {
e.printStackTrace();
}

huangapple
  • 本文由 发表于 2020年9月29日 00:21:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64105985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定