0

i Have a prob with a File on my Project Java My project runs on Eclipse But not a console mode

I Have 3 Packages

pk 1

-> class reads a File

   FileInputStream propFile= null;
      Properties properties = new Properties(System.getProperties());
        try {
            propFile = new FileInputStream("file.txt");

            properties.load(propFile);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

-> file.txt

pk2

pk3 -> my main

My code Works On Eclispe but if not at console mode

I don't understand why I can resolve this.

Hanny94
  • 125
  • 1
  • 1
  • 9

2 Answers2

0

Your code tries to read a file named file.txt from the current directory. So, before launching the program in the console, type the command

ls

on Unix or

dir

on Windows. If there is no file.txt listed, then your code will throw a FileNotFoundException.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Complement to @JBNizet's answer; here is what you can do using the new file API:

final Path propertiesFile = Paths.get("file.txt").toAbsolutePath();
final InputStream(in) = Files.newInputStream(propertiesFile);
props.load(in);

If the file does not exist, you will get a NoSuchFileException. If you don't have access to it, you will get an AccessDeniedException. Things that the old, legacy file API will not distinguish!

Side note: have a look here. This will give you lots of reasons to switch to the Files API.

fge
  • 119,121
  • 33
  • 254
  • 329