Isn’t annoying to remember all the mumbo jumbo just to open a file and what about reading a file into a string, colloquially known as slurping.
package com.java.files; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class files { // open resource as BufferedReader // Example usage : // BufferedReader br = open("file_name.txt"); // .... do something .. br.readLine() ... // br.close(); public static BufferedReader open(Class ref, String file_name) throws FileNotFoundException { InputStream stream = ref.getResourceAsStream(file_name); if (stream == null) throw new FileNotFoundException("Can't locate : " + file_name); BufferedReader br = new BufferedReader(new InputStreamReader(stream)); return br; } // open file as BufferedReader public static BufferedReader open(String file_name) throws FileNotFoundException { BufferedReader br = new BufferedReader(new FileReader(file_name)); return br; } public static void write2file(String file_name, String str) throws IOException { BufferedWriter wr = new BufferedWriter(new FileWriter(file_name)); wr.write(str); wr.close(); } //internal function to process a file for the slurps... private static String slurpit(BufferedReader br, String skip, String eol) throws Exception { StringBuilder sb = new StringBuilder(); String line; while( (line = br.readLine() ) != null ) { if(skip == null || (skip != null && !line.matches(skip)) ) { if (eol == null) { sb.append( line ); } else { sb.append(line + eol); }; } }; br.close(); return sb.toString(); } //read a whole file into a string, but skip lines that match a regex public static String slurp_skip(String file_name, String skip, String eol) throws Exception { BufferedReader br = open(file_name); return slurpit(br, skip, eol); } //read a whole file into a string, from a "jar" directories i.e. relative. // but skip lines that match a regex public static String slurp_skip(Class ref, String file_name, String skip, String eol) throws Exception { BufferedReader br = open(ref,file_name);//read resource return slurpit(br, skip, eol); } //just read the whole file in a string public static String slurp(String file_name) throws Exception { return slurp_skip(file_name, null, null); } public static String slurp(Class ref, String file_name) throws Exception { return slurp_skip(ref, file_name, null, null); } public static String slurp(Class ref, String file_name, String eol) throws Exception { return slurp_skip(ref, file_name, null, eol); } public static String slurp(String file_name, String eol) throws Exception { return slurp_skip(file_name, null, eol); } }
Example usage :
//print the first line of a file BufferedReader br = open("file_name.txt"); System.out.println( br.readLine() ) br.close(); //print a file System.out.println( slurp("file_name.txt") ) //print a file from directory relative to the .jar root or resources dir System.out.println( slurp(this.getClass(), "file_name.txt") )