2

I would like to read an entire text file and store its entire contents into a single string. Then I would like to print the string to the console window. I tried this:

import java.util.Scanner;
import java.io.*;

public class WritingTextFiles{

    public static void main (String [] args) throws IOException{
        FileWriter fw= new FileWriter("testing.txt");  
        Scanner in= new Scanner (System.in);
        String testwords=in.nextLine();  
        fw.write(testwords);  
        BufferedReader r = new BufferedReader( new FileReader( "testing.txt" ) );  
        System.out.print(r);  
        fw.close();  
    }
}

The only thing that is printed to the console window is java.io.BufferedReader@18fb397.

Can anyone explain this to a newbie like me? I have very little experience but I am certainly willing to learn. I am open to any and all suggestions. Thanks in advance!

user unknown
  • 35,537
  • 11
  • 75
  • 121
user1299661
  • 183
  • 2
  • 3
  • 11
  • Look at the `read()` and `readLine()` methods in the BufferedReader class' API: http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html – Hunter McMillen Apr 24 '12 at 22:44
  • http://stackoverflow.com/questions/3886201/java-outputting-text-file-to-console – assylias Apr 24 '12 at 22:45

2 Answers2

6

The reason that java.io.BufferedReader@18fb397 is printed to the console is because you give the reference of the buffered reader as an argument to print, and not the string you want to print.

BufferedReader r = new BufferedReader( new FileReader( "testing.txt" ) );
System.out.print(r);

should be:

BufferedReader r = new BufferedReader( new FileReader( "testing.txt" ) );
String s = "", line = null;
while ((line = r.readLine()) != null) {
    s += line;
}
System.out.print(s);

Notice we actually read the lines of the file and store it in a temporary variable, then we append this variable to s. Then we print s, and not the BufferedReader.

On a final note, it is wise to close a file when your done, you do call fw.close(), but you should have called it directly after writing the testwords. This is to make sure that the FileWriter has actually written the string.

Maarten Blokker
  • 604
  • 1
  • 5
  • 23
  • The program would not compile. I am recieveing an incompatible types error which requires a boolean on line: while((line=r.readLine()!=null) { Can you help me? – user1299661 Apr 25 '12 at 02:44
  • Off course, sorry mate, i wrote this code without testing it. I editted my original answer to compile correctly :) – Maarten Blokker Apr 25 '12 at 09:18
  • I have and thank you! Is there a way to read a file and store it into a type other than a string which can also be read in an array? I have a program that takes the contents of a file and organizes it into an array, then it takes a user's input and compares it to the components of the file, but with a string, it takes huge amounts of time to deal with large text files. Any advice? – user1299661 Apr 26 '12 at 12:16
  • Oh sounds like a nice project, you can read chunks of the file, say 250 lines and store them in an array or your own chunk object perhaps. Then just compare chunks you read, you dont have to load the entire text file at once this way, and this will scale very well. – Maarten Blokker Apr 26 '12 at 12:50
  • How would you read a particular amount of lines from the file? – user1299661 Apr 26 '12 at 13:39
  • Well thats a bit more complicated, and i feel you should ask that in a different question. It involves limiting your loop and store an offset value somewhere, then parsing the chunks. – Maarten Blokker Apr 26 '12 at 15:38
0

If it's a relatively small file, a one-line Java 7+ way to do this is:

System.out.println(new String(Files.readAllBytes(Paths.get("testing.txt"))));

If you just want to read it into a String, that's also simple:

String s = new String(Files.readAllBytes(Paths.get("testing.txt")));

See https://docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html for more details.

Cheers!

Ari
  • 61
  • 4