gpt4 book ai didi

java - 使用 java 属性文件时出现 FileNotFoundException

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:42:42 25 4
gpt4 key购买 nike

我在做了很多研究之后问了这个问题,并在研究之后在我的代码中实现了它,但我最终得到了 FileNotFoundException。我在这里做的是我想避免在我的 java 代码中进行硬编码,所以我创建了一个属性名为 Constants.properties 的文件,并在我的 Java 代码中调用它。但它说它没有找到该文件。我的属性文件在项目的 src 文件夹中。下面是代码片段。有什么建议吗?

属性文件:

executable.run = C:\\server\\lrd.exe
incoming.file = C:\\file\\test.ic
executable.params1 = -z
executable.params2 = -a[+]
log.file = C:\\TESTFile\\test.txt

Java 代码:这是包含属性文件详细信息的类文件。

public class PropInfo {
static private PropInfo _instance = null;
public String executable = null;
public String filein = null;
public String params1 = null;
public String params2 = null;
public String log = null;

protected PropInfo(){
try{
InputStream file = new FileInputStream(new File("Constants.properties"));
Properties props = new Properties();
props.load(file);
executable = props.getProperty("executable.run");
filein = props.getProperty("incomin.file");
params1 = props.getProperty("executable.params1");
params2 = props.getProperty("executable.params2");
log = props.getProperty("log.file");
}
catch(Exception e){
System.out.println("error" + e);
}
}

static public PropInfo instance(){
if(_instance == null){
_instance = new PropInfo();
}
return _instance;
}
}

主类:

try{
PropInfo propinfo = PropInfo.instance();
String connString = propinfo.executable + " " + propinfo.params1 + " " +
propinfo.filein + " " + propinfo.params2 + " " + " " + propinfo.log ;

Runtime rt = Runtime.getRuntime();
// Process pr = rt.exec
// (PropInfo.executable+" "+PropInfo.params1+" "+PropInfo.filein+" "
//+PropInfo.params2+" "+PropInfo.log);
Process pr = rt.exec(connString);

BufferedReader input = new BufferedReader(new InputStreamReader (pr.getInputStream()));

String line=null;
StringBuffer start= new StringBuffer();
while((line=input.readLine()) != null) {
start.append("Started" + line + "\n");
System.out.println(line);
}

// System.out.println("browse");

}
catch (Throwable t)
{
t.printStackTrace();
}
finally
{
}

给出这个异常(exception):

errorjava.io.FileNotFoundException: Constants.properties (The system cannot find the  
file specified)
java.io.IOException: Cannot run program "null": CreateProcess error=2, The system
cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at com.emc.clp.license.StartTest.main(StartTest.java:44)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the
file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:288)
at java.lang.ProcessImpl.start(ProcessImpl.java:133)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 4 more

最佳答案

是的,不要将您的属性文件放入 src 文件夹中。把它放在你启动 jvm 的地方(或提供一个绝对路径)。另外,我真的建议去掉路径名中的正斜杠。

更新:添加这个以找出放置文件的位置:

System.out.println(new File(".").getAbsolutePath());

关于java - 使用 java 属性文件时出现 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19304651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com