文件路径给出正确的,但文件显示错误文件找不到

问题描述:

我创建的小应用程序,它是使用JavaFX的上保存数据......问题出我,当我运行我的应用程序是没有找到该文件.. 错误对于错误行消息文件路径给出正确的,但文件显示错误文件找不到

`java.io.FileNotFoundException: null\sample-app.conf (The system cannot find the path specified)` 

代码如下

private String configFile = System.getProperty("user.home") + File.separator + "sample-app.conf"; 

public boolean loadLicense() { 
     // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    try { 
      properties.load(new FileInputStream(configFile)); 

      licenseString = properties.getProperty("license-string"); 
      activatedLicenseString = properties.getProperty("activated-license-string"); 
      if (properties.getProperty("license-type") != null) { 
       licenseType = Integer.parseInt(properties.getProperty("license-type")); 
      } 
      if (properties.getProperty("activated-license-type") != null) { 
       activatedLicenseType = Integer.parseInt(properties.getProperty("activated-license-type")); 
      } 

      hostname = properties.getProperty("floating-license-server-hostname"); 
      if (properties.getProperty("floating-license-server-port") != null) { 
       int portnumber = Integer.parseInt(properties.getProperty("floating-license-server-port")); 
      } 

      return true; 
     } catch (IOException ex) { 
      Logger.getLogger(RegController.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     return false; 
    } 
+3

'System.getProperty(“C:\\用户\\联想”)'ISN”不要做你想做的事。目前还不清楚你希望这个返回,但它返回null。 – jdv

+0

@jdv我正在配置文件中创建一个基于小应用程序的数据保存...但我给路径也存在,但显示错误消息,“系统找不到指定的路径” –

+0

C:\ users \ leveno是到达文件的路径 –

目前还不清楚为什么你正在构建使用System.getProperty("C:\\Users\\Lenovo")路径但这几乎肯定返回null。这可能是你问题的根源,尽管你没有提供堆栈跟踪,但是不可能确定。

这是因为System.getProperty()intended to return a value for a property key您传递的,而“C:\用户\联想”不是一个属性键(除非你是一个-D选项设置它作为一个属性运行JVM)。

所以,解决方案取决于你想要做什么。

  1. 也许这个.conf文件是总是在同一个位置。如果是这种情况,请将configFile设置为路径名的固定字符串版本。
  2. 也许你想在-D "my.license.conf.rootdir=C:\\Users\\Lenovo"传递给JVM,在这种情况下,你会用System.getProperty("my.license.conf.rootdir"),而不是构建您的路径的方式。
  3. 别的东西。你以另一种方式构建一条对你有意义的路径。

不管你做什么,一旦你有一个字符串路径名,你可以使用像File.exists()传递到实际期望的路径名特定的文件是存在于系统中的代码之前典型的API进行验证,并纠正你的码。

也就是说,你想,依次是:

  • 导出,组装,或定义路径,可能是因为某种字符串的
  • 验证此路径,所以你知道有一个具体的,上此路径名
  • 的-disk表示通过,以进一步代码此验证路径,无论是作为一个字符串或一些其它物体
+0

我编辑了代码,请检查 –