Results 1 to 7 of 7
- 03-12-2010, 12:50 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
How can I get the file contents to display as readable text?
I think the file is being read, but the display is nothing but numbers such as below: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."); } } }
72
101
108
108
111
32
87
...
Can anyone help me out with what it is that I am missing? Thanks in advance.
- 03-12-2010, 01:12 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 03-12-2010, 01:52 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
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.
- 03-12-2010, 03:00 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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.
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-
- 03-12-2010, 03:04 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
I appreciate the help and the positive advise. Thanks a bunch.
- 03-12-2010, 04:27 AM #6
hi
see what is the return type of br.read() in
BufferedReader (Java 2 Platform SE v1.4.2)
thank
please use br.readLine() ;
- 03-12-2010, 04:54 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Display partial file contents in JTextArea
By tmoehlman in forum New To JavaReplies: 0Last Post: 11-02-2009, 11:03 PM -
problem trying to display the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 17Last Post: 07-13-2009, 05:44 AM -
Display the contents of a file on jsp page
By shiva in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 03-30-2009, 01:01 PM -
problems trying to view the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 1Last Post: 07-18-2007, 11:20 PM -
viewing the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 0Last Post: 07-17-2007, 02:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks