public class Read {
public static void main(String[] args) {
System.out.println(doRead());
}
public static String doRead() {
String result = "";
try {
BufferedReader fr;
InputStream inputStream = Read.class.getResourceAsStream("/data.txt");
if (inputStream != null) {
fr = new BufferedReader(new InputStreamReader(inputStream));
} else {
fr = new BufferedReader(new FileReader(new File("resources/data.txt")));
}
result = fr.readLine();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
Tuesday, 23 August 2011
Reading Files cross jars
To my somewhat annoyance I discovered that reading files from the current package is done with a normal FileReader but if you want to read the same file from a different jar you have to use the getResourceAsStream() method.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment