2017-10-16 146 views
0

我有一个问题。在我的程序中,有一个类Firma。 在这个类的构造函数中,我从名为'firmendaten.fd'的文本文件中读取了一些信息。该文本文件也位于相同的包,但如果我尝试阅读我得到FileNotFoundException文件未发现异常,但它是在同一文件夹/包

产生错误的代码:

public Firma(){ 
    BufferedReader in = null; 
    try { 
     in = new BufferedReader(new FileReader("Firmendaten.fd")); 
     name = in.readLine(); 
     zusatz = in.readLine(); 
     strasse = in.readLine(); 
     hnr = in.readLine(); 
     plz = in.readLine(); 
     ort = in.readLine(); 
     bank = in.readLine(); 
     iban = in.readLine(); 
     bic = in.readLine(); 
     steuerNr = in.readLine(); 
     steuersatz = in.readLine(); 
     chef = in.readLine(); 
     zahlungsziel = in.readLine(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (in != null) 
      try { 
       in.close(); 
      } catch (IOException e) { 
      } 
    } 
    } 

错误它产生:

java.io.FileNotFoundException: Firmendaten.fd (Das System kann die angegebene Datei nicht finden) 
at java.io.FileInputStream.open0(Native Method) 
at java.io.FileInputStream.open(Unknown Source) 
at java.io.FileInputStream.<init>(Unknown Source) 
at java.io.FileInputStream.<init>(Unknown Source) 
at java.io.FileReader.<init>(Unknown Source) 
+1

请不要发布错误的截图,但以文本形式张贴,即产生错误(除非它是太多)代码和错误本身。 – Thomas

+0

尝试使用,当你经过它,而不是仅仅名称 –

+0

如果我使用绝对路径它的优良的文件的绝对路径,但绝对路径是错误代码,因为如果PROGRAMM位于其他地方再次抛出该异常。 – flam3shadow

回答

0

我最近也有类似的十岁上下的问题,所以我使用的.jar文件的位置,其中该程序正在运行。

 String path = Class.class.getProtectionDomain().getCodeSource().getLocation().getPath(); 
     path = path.substring(0, path.lastIndexOf("/") + 1); 
     String decodedPath = URLDecoder.decode(path, "UTF-8"); 
     return decodedPath; 

那时,我便像文件:

File file = new File(userPath + "\\yourfile.txt"); 

注意,这是从这里回答的混合服用,所以希望可以帮助你。

+0

在程序代码:。 字符串路径= Class.class.getProtectionDomain()getCodeSource()的getLocation()的getPath(); 如果我用你的代码片段 – flam3shadow

+0

我发现我在使用片断犯的错 – flam3shadow

相关问题