Sunday, August 28, 2011

Reading a data file as a Resource


Put the file to be read in the src/main/java/resource folder as shown on the right: eg. I put in the log4j.xml here, and I will read/output the file line by line.

Then you can access this resource from your code as following, remember to put the leading slash on the filename

InputStream fstream = this.getClass().getResourceAsStream("/log4j.xml");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
System.out.println(strLine);
}
in.close();



0 Comments:

Post a Comment

<< Home