Read a file outside of JAR in JAVA

March 12, 2016 | 1030 views | Comments

Whether it's a properties file, text file, log file, config file, to name a few, FileInputStream together with BufferedReader can be used to access and read a file outside of a JAR in JAVA.

 

try {
    /*
     * Set the file name with it's
     * specific location.
     */
    String file = "/PATH/FILE-NAME";
    FileInputStream fIs = new FileInputStream(new File(file));
    
    BufferedReader br = new BufferedReader(new InputStreamReader(fIs));
    try {
        String line;
        /*
         * Iterate through each line.
         */
        while ((line =  br.readLine()) != null) {
            /*
             * Do whatever is needed on the value of the line.
             */
           System.out.println(line);
        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
} catch (FileNotFoundException e1) {
    e1.printStackTrace();
}