Java 属性文件不工作
当我运行程序时,我得到以下错误日志:Java 属性文件不工作,java,properties,Java,Properties,当我运行程序时,我得到以下错误日志: java.io.FileNotFoundException: config.properties (Het systeem kan het opgegeven bestand niet vinden) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:138) at ja
java.io.FileNotFoundException: config.properties (Het systeem kan het opgegeven bestand niet vinden)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:97)
at Manuals.<init>(Manuals.java:62)
at Manuals.main(Manuals.java:479)
Exception in thread "main" java.lang.NullPointerException
at Manuals.getProjects(Manuals.java:372)
at Delete.<init>(Delete.java:27)
at Manuals.run(Manuals.java:90)
at Manuals.main(Manuals.java:479)
当堆栈跟踪在你面前尖叫时:必须先创建文件,然后才能使用访问它 您可以创建文件,而不只是记录异常。但是检查它的存在会更干净,因为在这种情况下,
FileNotFoundException
实际上是多个异常的容器()
我在想这样的事情:
public Manuals() throws IOException {
File physicalFile = new File("config.properties");
if(!physicalFile.exists()) {
physicalFile.createNewFile();
}
// at this point we either confirmed that the file exists or created it
FileInputStream file = new FileInputStream(physicalFile);
Properties configFile = new Properties();
configFile.load(file);
curPdf = new ArrayList();
addPdf = new ArrayList();
allPdf = new ArrayList();
this.search = "";
}
您的
config.properties
文件在哪里?您编写的代码希望它位于当前目录(通常是您调用java
程序的目录)中。输入此项时,将自动创建文件?不,不会。您说过程序创建了文件,但您的代码没有,您在调用load
之前是否使用store
方法?这是构造函数,没有,我不是在load
之前调用store
,这是一个不同的问题/错误,它会影响您在问题中未编写的部分代码。原来的问题解决了。
public Manuals() throws IOException {
File physicalFile = new File("config.properties");
if(!physicalFile.exists()) {
physicalFile.createNewFile();
}
// at this point we either confirmed that the file exists or created it
FileInputStream file = new FileInputStream(physicalFile);
Properties configFile = new Properties();
configFile.load(file);
curPdf = new ArrayList();
addPdf = new ArrayList();
allPdf = new ArrayList();
this.search = "";
}