Results 1 to 7 of 7
  1. #1
    xtiano77 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Question How can I get the file contents to display as readable text?

    Java Code:
    import java.io.*;
    
    public class TestClass {
    	public static void main (String args[]) throws IOException {
    		try{
    			BufferedReader br = new BufferedReader(new FileReader("testFile.txt"));
    			char txt;
    			while(br.ready()){
    				System.out.println(br.read());
    			}
    		}catch(IOException e){
    			System.out.println("IOException caught.");
    		}
    	}	
    }
    I think the file is being read, but the display is nothing but numbers such as below:

    72
    101
    108
    108
    111
    32
    87
    ...

    Can anyone help me out with what it is that I am missing? Thanks in advance.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Does the file happen to begin with "Hello W"? Do you wonder how I know that?

    Take another look at the BufferedReader docs, particularly the read() and readLine() methods.

    -Gary-

  3. #3
    xtiano77 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    Yes, it began with "Hello World". The whole text was "Hello World! This is a test on how to write to a file while usign Java.". I think this would be a simpler approach, right? I guess my biggest hang up is perhaps the fact that I get a bit intimidated by the large amount of classes and packages.

    Java Code:
    import java.io.*;
    
    public class TestClass {
    	public static void main (String args[]) throws IOException {
    		try{
    			FileReader fr = new FileReader("testFile.txt");
    			while(fr.ready()){
    				System.out.print((char) fr.read());
    			}
    		}catch(IOException e){
    			System.out.println("IOException caught.");
    		}
    	}	
    }
    Last edited by xtiano77; 03-12-2010 at 02:33 AM.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Yeah, you got it. With the BufferedReader's read() method you were reading bytes, and then your println() was printing the numeric values -- 72 = H, 101 = e, 108 = l, etc. You didn't have to switch to a FileReader, and depending on what else you wanted to do, you may have been happier with the BufferedReader's readLine() method.

    Quote Originally Posted by xtiano77 View Post
    I guess my biggest hang up is perhaps the fact that I get a bit intimidated by the large amount of classes and packages.
    I totally understand that. Just know that nobody came out of the womb knowing this stuff -- everybody had to learn these classes and libraries one at a time. Be patient with yourself, and think of it like making new friends.

    -Gary-

  5. #5
    xtiano77 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    I appreciate the help and the positive advise. Thanks a bunch.

  6. #6
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    160
    Rep Power
    5

    Default

    hi
    see what is the return type of br.read() in
    BufferedReader (Java 2 Platform SE v1.4.2)
    thank
    please use br.readLine() ;

  7. #7
    xtiano77 is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Display partial file contents in JTextArea
    By tmoehlman in forum New To Java
    Replies: 0
    Last Post: 11-02-2009, 11:03 PM
  2. Replies: 17
    Last Post: 07-13-2009, 05:44 AM
  3. Display the contents of a file on jsp page
    By shiva in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-30-2009, 01:01 PM
  4. Replies: 1
    Last Post: 07-18-2007, 11:20 PM
  5. viewing the contents of a text file in JTextArea
    By warship in forum New To Java
    Replies: 0
    Last Post: 07-17-2007, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •