0

I have a problem. In my program in one package there are a class Firma. At the constructor of this class I read some information out of a text file named 'firmendaten.fd'. The textfile is also located at the same package, but if I try to read i get a FileNotFoundException.

The code which produces the error:

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

The error it produces:

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)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Please don't post screenshots of errors but post them in text form, i.e. the code that produces the error (unless it's too much) and the error itself. – Thomas Oct 16 '17 at 15:19
  • try using the absolute path of the file when you pass it in instead of just the name – RAZ_Muh_Taz Oct 16 '17 at 15:27
  • if i use the absolute path it's fine, but the absolute path is bad code, because if the programm is located somewhere else it throws this exception again. – flam3shadow Oct 16 '17 at 15:34
  • 1
    You're readng a file. That has nothing to do with packages. Relative paths start with the current directory, i.e. the directory from which the java command is executed. Just like `less foo.txt` reads the foo.txt file in the curretnt directory. If you want to load a resource from the classpath, then don't use file IO. Use `Firma.class.getResourceAsStream("Firmendaten.fd")` – JB Nizet Oct 16 '17 at 15:42
  • the command is executed at Rechnungsprogramm\src\main\Firma.java the file which should be read is Rechnungsprogramm\src\main\Firmendaten.fd so in my mind the relative path should be Firmendaten.fd – flam3shadow Oct 16 '17 at 15:50

1 Answers1

0

I had a similar-ish problem recently so I used the location of the .jar file of which the program was running.

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

Then I called the file like:

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

Note that this was taken from a mix of answers on here, so hopefully may help you.

achAmháin
  • 4,176
  • 4
  • 17
  • 40