Monday, April 28, 2014

Where am I?

While calling something like

FileInputStream stream = new FileInputStream("myfile.properties");

you might see Filenotfound exception... But it used to work before, you might say. Well, it probably did before you called this method from a different directory. Use following statement to get user's current working directory.

System.getProperty("user.dir")

1 comment:

  1. In order to load let's say property file that you would like within your program. Put a file into /src/main/resources and call it as

    String sCurrentLine;

    InputStream in = this.getClass().getClassLoader()
    .getResourceAsStream("stopwords.txt");
    try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    while ((sCurrentLine = reader.readLine()) != null) {
    System.out.println("Processing " + sCurrentLine);
    }
    }
    catch (NullPointerException nulle) {
    LOG.log(SEVERE, "Threw null pointer. Could not read the file!");
    }
    catch (IOException e) {
    LOG.log(SEVERE, "Threw IOException. Could not read the file!");
    }

    ReplyDelete