英文:
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("C:\\numbers.txt");
File positivNumbers = new File("C:\\positivnumbers.txt");
File negativNumbers = new File("C:\\negativnumbers.txt");
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 <= 100; i++) {
wr.write(String.valueOf((i) + " "));
}
for (int a = -1; a >= -100; a--) {
wr.write(((a) + " "));
}
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 <= 100; i++) {
wr.write(String.valueOf((i) + " "));
}
for (int a = -1; a >= -100; a--) {
wr.write(((a) + " "));
}
} 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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论