使用java属性文件时的FileNotFoundException [英] FileNotFoundException when using java properties file

查看:193
本文介绍了使用java属性文件时的FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

属性文件:

  executable.run = C :\\\\server\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ .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;

保护PropInfo(){
尝试{
InputStream file = new FileInputStream(new File(Constants.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;




$ b $主要类别:

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

运行时rt = Runtime.getRuntime();
// Process pr = rt.exec
//(PropInfo.executable ++ PropInfo.params1 ++ PropInfo.filein +
//+PropInfo.params2++ PropInfo .LOG);
进程pr = rt.exec(connString);

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

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

// System.out.println(browse);

$ b catch(Throwable t)
{
t.printStackTrace();

finally


code


这个例外:
$ b $ pre $ errorjava.io.FileNotFoundException:Constants.properties(系统找不到指定的
文件)
java.io.IOException:无法运行程序null:CreateProcess error = 2,系统
在java.lang.ProcessBuilder.start找不到指定的
文件(ProcessBuilder.java:1042 )java.lang.Runtime.exec上的
(Runtime.java:615)java.lang.Runtime.exec上的
(Runtime.java:448)java.lang.Runtime上的
。 exec(Runtime.java:345)
at com.emc.clp.license.StartTest.main(StartTest.java:44)
导致:java.io.IOException:CreateProcess error = 2,系统无法在java.lang.ProcessImpl.create(Native Method)
处找到指定

文件(< init>(ProcessImpl.java:288)
at java.lang.ProcessImpl.start(ProcessImpl.java:133)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 4 more


解决方案

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

更新:添加这个来找出你的文件的位置:

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


am asking this question after doing a lot of research and also implementing it in my code after the research but I ended up with FileNotFoundException.What exactly am doing here is I want to avoid hardcoding in my java code so am creating a properties file with name as Constants.properties and am calling it in my java code. but it says that it is not finding the file. My properties file is in the src folder of the project. Below is the code snippet. Any suggestions?

Properties file:

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 Code: This is the class file which has the properties file details.

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;
    }
}

Main Class:

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 
{  
}

Gives this 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

解决方案

Yes, don't put your properties file into the src folder. Put it where you start the jvm from (or provide an absolute path). Also I really suggest getting rid of forward slashes in path names.

UPDATE: Add this to find out where to put your file:

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

这篇关于使用java属性文件时的FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆