[英]Try Catch logic error with file i/o in Java
So, I've been trying to learn java from various sources, I've been learning for about 2 years now. 因此,我一直在尝试从各种来源学习Java,我已经学习了大约2年。 So far everything has been going smoothly, i haven't had to post on stackoverflow for a while.
到目前为止,一切都进行得很顺利,我已经有一段时间没有发布过stackoverflow了。 Recently I've been trying to figure out how to create and read files with java.
最近,我一直在尝试弄清楚如何使用java创建和读取文件。 I can do both of those things in separate apps, but when i try to do both it doesn't always work.
我可以在单独的应用程序中同时执行这两项操作,但是当我尝试执行这两项操作时并不总是有效。
What i want to happen: I want my program to create data.txt
, then I want it to read the data and produce an error log on error.txt
. 我想发生的事情:我希望程序创建
data.txt
,然后希望它读取数据并在error.txt
上产生错误日志。
What happens: The data.txt file gets created as expected, but nothing is written to the error.txt file. 发生的情况:data.txt文件已按预期创建,但是没有任何内容写入error.txt文件。 I'm having trouble grasping the try/catch block and how exactly it works.
我在获取try / catch块及其工作原理方面遇到麻烦。 Anyone got any ideas?
任何人有任何想法吗? even just some advice would be appreciated.
即使只是一些建议也将不胜感激。 Thanks!
谢谢!
import java.io.*;
import java.util.Scanner;
public class dataReader {
public static void main(String args[]) throws Exception {
File fileName;
fileName = new File("data.txt");
PrintWriter outputFile;
outputFile = new PrintWriter(fileName);
File errorFile;
errorFile = new File("errors.txt");
PrintWriter outputErrorFile;
outputErrorFile = new PrintWriter(errorFile);
Scanner inputFile;
int recordNumber = 0;
String inputData;
outputFile.println(77);
outputFile.println("Fred");
outputFile.println(92);
outputFile.println("Wilma");
outputFile.println(89.9);
outputFile.println("Barney");
outputFile.println(42);
outputFile.println("BettyS");
inputFile = new Scanner(fileName);
while (inputFile.hasNext()) {
recordNumber++;
try {
inputData = inputFile.nextLine();
if (Integer.parseInt(inputData) < 50) {
outputErrorFile.println(recordNumber + ", " + inputData + ", is less than 50.");
} else if (Integer.parseInt(inputData) > 90) {
outputErrorFile.println(recordNumber + ", " + inputData + ", is less than 50.");
}
} catch (Exception e) {
outputErrorFile.println(recordNumber + ", That's not an integer.");
}
}
outputFile.close();
outputErrorFile.close();
System.out.println("Program terminated.");
}
}
Move the outputFile.close();
移动
outputFile.close();
line before inputFile = new Scanner(fileName);
inputFile = new Scanner(fileName);
之前的行inputFile = new Scanner(fileName);
. 。 Currently it's just cached in the memory and not written actually to the disk.
目前,它只是缓存在内存中,实际上并未写入磁盘。
The documentation of PrintWriter says it all. PrintWriter的文档说明了一切。 The PrintWriter(Writer) constructor creates a writer which is not automatically flushed.
PrintWriter(Writer)构造函数创建一个不会自动刷新的writer。
You have to call close or flush method to write your data to the file. 您必须调用close或flush方法将数据写入文件。
So you have to use outputFile.close();
因此,您必须使用
outputFile.close();
method before starting reading. 开始阅读之前的方法。
and as a good practice you have to close all your PrintWriter
instances to avoid memory leak. 作为一种好习惯,您必须关闭所有
PrintWriter
实例,以避免内存泄漏。
just in this case please add inputFile.close();
仅在这种情况下,请添加
inputFile.close();
at the end of your program. 在程序结束时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.