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;
}
}
Showing posts with label file. Show all posts
Showing posts with label file. Show all posts
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.
Monday, 26 July 2010
How 2 read a textfile the easy way
Reading in a text file should be so easy. However Java's designers went a bit nuts with the decorator pattern. Fortunately commons.IO makes life easier:
http://commons.apache.org/io/api-release/index.html
Has this lovely method:
also of note:
http://commons.apache.org/io/api-release/index.html
Has this lovely method:
public static String readFileToString(File file)
also of note:
public static byte[] readFileToByteArray(File file)
public static String readFileToString(File file, String encoding)
Subscribe to:
Posts (Atom)