Reputation: 469
I have a text file named foo.txt
, and its contents are as below:
this
is
text
How would I print this exact file to the screen in Java 7?
Upvotes: 34
Views: 199468
Reputation: 1647
I just want to add a few alternatives with Apache Commons:
using org.apache.commons.io.FileUtils
:
File file = new File(getClass().getClassLoader().getResource("foo.txt").getFile());
String text = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
using org.apache.commons.io.IOUtils
:
FileInputStream fis = new FileInputStream("foo.txt");
String text = IOUtils.toString(fis, StandardCharsets.UTF_8);
foo.txt in classpath e.g. main/java/resources
Upvotes: 0
Reputation: 12837
Before Java 7:
BufferedReader br = new BufferedReader(new FileReader("foo.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
Since Java 7, there is no need to close the stream, because it implements autocloseable
try (BufferedReader br = new BufferedReader(new FileReader("foo.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
Upvotes: 52
Reputation: 976
For those new to Java and wondering why Jiri's answer doesn't work, make sure you do what he says and handle the exception or else it won't compile. Here's the bare minimum:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
for (String line; (line = br.readLine()) != null;) {
System.out.print(line);
}
br.close()
}
}
Upvotes: 2
Reputation: 10291
Every example here shows a solution using the FileReader. It is convenient if you do not need to care about a file encoding. If you use some other languages than english, encoding is quite important. Imagine you have file with this text
Příliš žluťoučký kůň
úpěl ďábelské ódy
and the file uses windows-1250 format. If you use FileReader you will get this result:
P��li� �lu�ou�k� k��
�p�l ��belsk� �dy
So in this case you would need to specify encoding as Cp1250 (Windows Eastern European) but the FileReader doesn't allow you to do so. In this case you should use InputStreamReader on a FileInputStream.
Example:
String encoding = "Cp1250";
File file = new File("foo.txt");
if (file.exists()) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding))) {
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
else {
System.out.println("file doesn't exist");
}
In case you want to read the file character after character do not use BufferedReader.
try (InputStreamReader isr = new InputStreamReader(new FileInputStream(file), encoding)) {
int data = isr.read();
while (data != -1) {
System.out.print((char) data);
data = isr.read();
}
} catch (IOException e) {
e.printStackTrace();
}
Upvotes: 3
Reputation: 4217
Why hasn't anyone thought it was worth mentioning Scanner?
Scanner input = new Scanner(new File("foo.txt"));
while (input.hasNextLine())
{
System.out.println(input.nextLine());
}
Upvotes: 25
Reputation: 22271
With Java 7's try-with-resources Jiri's answer can be improved upon:
try (BufferedReader br = new BufferedReader(new FileReader("foo.txt"))) {
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
Add exception handling at the place of your choice, either in this try
or elsewhere.
Upvotes: 2