tags:

views:

590

answers:

6

How do you read and display data from .txt files?

+6  A: 

In general:

  • Create a FileInputStream for the file.
  • Create an InputStreamReader wrapping the input stream, specifying the correct encoding
  • Optionally create a BufferedReader around the InputStreamReader, which makes it simpler to read a line at a time.
  • Read until there's no more data (e.g. readLine returns null)
  • Display data as you go or buffer it up for later.

If you need more help than that, please be more specific in your question.

Jon Skeet
+6  A: 

If your file is strictly text, I prefer to use the java.util.Scanner class.

You can create a Scanner out of a file by:

Scanner fileIn = new Scanner(new File(thePathToYourFile));

Then, you can read text from the file using the methods:

fileIn.nextLine(); // Reads one line from the file
fileIn.next(); // Reads one word from the file

And, you can check if there is any more text left with:

fileIn.hasNext(); // Returns true if there is another word in the file
fileIn.hasNextLine(); // Returns true if there is another line to read from the file

Once you have read the text, and saved it into a String, you can print the string to the command line with:

System.out.print(aString);
System.out.println(aString);

The posted link contains the full specification for the Scanner class. It will be helpful to assist you with what ever else you may want to do.

jjnguy
I always forget about Scanner, never having actually used it... Shame it takes a charset *name* though :( (And I'd recommend always explicitly specifying the charset)
Jon Skeet
Well, I am assuming the simplest possible scenario. Adding charsets in can get confusing. Usually you don't have to worry about it.
jjnguy
But Scanners are great for quick and dirty text file reading.
jjnguy
I very rarely want to make my code system-dependent, which is what happens if you omit the charset. If I *did* want to use the default charset, I'd do so explicitly (Charset.defaultCharset().name()) Character encodings are a pain, but the sooner they're understood the better IMO.
Jon Skeet
I guess I'd rather pander to what I think the question asker is looking for. He/she probably looking to make their code system-independent.
jjnguy
A: 

You most likely will want to use the FileInputStream class:

int character;
StringBuffer buffer = new StringBuffer("");
FileInputStream inputStream = new FileInputStream(new File("/home/jessy/file.txt"));

while( (character = inputStream.read()) != -1)
        buffer.append((char) character);

inputStream.close();
System.out.println(buffer);

You will also want to catch some of the exceptions thrown by the read() method and FileInputStream constructor, but those are implementation details specific to your project.

craig
inputStream.read() doesn't return a *character* - it returns a *byte* (as an int, admittedly, but they're very different things). To read text, you should be using a Reader.
Jon Skeet
A: 

If you want to take some shortcuts you can use Apache Commons IO:

import org.apache.commons.io.FileUtils;

String data = FileUtils.readFileToString(new File("..."), "UTF-8");
System.out.println(data);

:-)

Vladimir
+2  A: 

I love this piece of code, use it to load a file into one String:

File file = new File("/my/location");
String contents = new Scanner(file).useDelimiter("\\Z").next();
Greg Noe
+5  A: 
BufferedReader in = new BufferedReader(new FileReader("<Filename>"));

Then, you can use in.readLine(); to read a single line at a time. To read until the end, write a while loop as such:

String line;
while((line = in.readLine()) != null)
{
    System.out.println(line);
}
kevmo314