Results 1 to 20 of 26
Thread: FileReader reads all data as int
- 09-14-2011, 05:07 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 26
- Rep Power
- 0
FileReader reads all data as int
Hi, I'm trying to use FileReader to read in a CSV file and print it in the console. My code
And the output is a bunch of integers. I'm trying to follow the tutorial at Character Streams (The Java™ Tutorials > Essential Classes > Basic I/O) but I don't think I'm getting the expected result. What did I do wrong?Java Code:import java.io.FileReader; import java.io.FileWriter; import java.io.BufferedReader; import java.io.PrintWriter; import java.io.IOException; public class fileComp { public static void main(String args[]) throws IOException { System.out.println("Hello"); FileReader original = null, modified = null; try { original = new FileReader("1.csv"); modified = new FileReader("2.csv"); int line; while ((line = original.read()) != -1) { System.out.println(line); } } finally { if (original == null || modified == null) { System.out.println("The original and/or modified file(s) could not be loaded."); } else { original.close(); modified.close(); } } } }
- 09-14-2011, 05:28 PM #2
Re: FileReader reads all data as int
Your code is reading the bytes of the file into an int variable.Java Code:int line; while ((line = original.read()) != -1) {
The read method you are using returns a int value.getting the expected result
What do you want to happen when you read the file?
If you want to read each line of the file into a String, look at wrapping the FileReader in a BufferedReader.Last edited by Norm; 09-14-2011 at 05:30 PM.
- 09-14-2011, 05:29 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 26
- Rep Power
- 0
Re: FileReader reads all data as int
The file has numbers and letters and I'd like to display it from the file to the console. I read Byte Streams (The Java™ Tutorials > Essential Classes > Basic I/O) as well and somehow they seem to be able to read as int and print it as char ... Is it because char is a type of int like C?
- 09-14-2011, 05:31 PM #4
Re: FileReader reads all data as int
If you want to read each line of the file into a String, look at wrapping the FileReader in a BufferedReader.
- 09-14-2011, 05:31 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: FileReader reads all data as int
You're not reading lines.
You're reading (as you've said) characters.
If you notice, in the example on that tutorial page, it's using write()...which writes a single character.
You are using println(), which adds a newline after each print (ie each character).
You want the second part of that tutorial, Buffered Streams, frankly.
- 09-14-2011, 05:32 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: FileReader reads all data as int
Gah!
Ninja'd!
:)
- 09-14-2011, 05:33 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 26
- Rep Power
- 0
Re: FileReader reads all data as int
Thanks, that is how I've implemented it so far, however I'd like to know if it's possible to read byte by byte even if it's a char or int? Basically this program is to compare the differences between 2 files. Currently, with BufferedReader, I am comparing it line by line, however I'd like to know how to compare it character by character as well, preferrably without converting the char to int.
- 09-14-2011, 05:35 PM #8
Member
- Join Date
- Sep 2011
- Posts
- 26
- Rep Power
- 0
Re: FileReader reads all data as int
Thanks. I don't mind the newline part of println, however I just want to know how to print out what I read as not all int. My file contains integers and characters, however when I read it in and print it to console, it shows up as all numbers. I'd like the characters to remain characters when I print them out and have the ability to compare character by character. Thanks.
- 09-14-2011, 05:35 PM #9
Re: FileReader reads all data as int
That is what the code in your first post did.I'd like to know if it's possible to read byte by byte
Do you understand that the value of '1' is not 1?
- 09-14-2011, 05:36 PM #10
Member
- Join Date
- Sep 2011
- Posts
- 26
- Rep Power
- 0
- 09-14-2011, 05:37 PM #11
Re: FileReader reads all data as int
No I think you want all the values printed out as characters.
Try casting the int to char when you print them.
Do you understand that the value of character '1' is not 1?
- 09-14-2011, 05:40 PM #12
Member
- Join Date
- Sep 2011
- Posts
- 26
- Rep Power
- 0
- 09-14-2011, 05:41 PM #13
Re: FileReader reads all data as int
I'm assuming that There are no binary numbers in your file. Everything in the file is a character.
Try this:
System.out.println("65=" + (char)65 + " '1'=" + (int)'1'); // 65=A '1'=49
- 09-14-2011, 05:43 PM #14
Member
- Join Date
- Sep 2011
- Posts
- 26
- Rep Power
- 0
- 09-14-2011, 05:46 PM #15
Re: FileReader reads all data as int
Define what you mean by a character?
For ASCII each byte is one character
For UNICODE there are two bytes for a character
There are other schemes that can use more bytes for a character.
You have to know the file's encoding to be able to convert its contents to characters.
- 09-14-2011, 06:54 PM #16
Member
- Join Date
- Sep 2011
- Posts
- 26
- Rep Power
- 0
Re: FileReader reads all data as int
By character I mean each symbol (letter, number, etc.) the way it is written when I open it with say NotePad. For example, I would use fscanf() in C to read the letters/numbers/symbols from a file the way it appears when opened with a text editor.
How would I hardcode the encoding in Java? I'm in North America so ...
- 09-14-2011, 06:57 PM #17
Re: FileReader reads all data as int
The characters that are displayed in some editor come from one or more bytes of the file depending on the encoding.
A character is something that is displayed for human viewing. The contents of the file is what defines what character is displayed.
Given an encoding for the bytes of a file, the file can be read and its bytes converted into characters for viewing.
- 09-14-2011, 07:00 PM #18
Member
- Join Date
- Sep 2011
- Posts
- 26
- Rep Power
- 0
- 09-14-2011, 07:05 PM #19
Re: FileReader reads all data as int
Every thing in a text file is a character. EVERYTHING. There are no numbers. '1' is not a number.
Or from another point of view, every byte in a text file has a binary value from 0 to 255.
Your original code read the file as bytes and printed their values.
- 09-14-2011, 07:08 PM #20
Member
- Join Date
- Sep 2011
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
Write a Program that reads two times in military format
By HPcompaq256 in forum New To JavaReplies: 10Last Post: 02-26-2010, 04:38 AM -
add FileReader to GUI
By VinTiger in forum New To JavaReplies: 8Last Post: 05-11-2009, 12:23 AM -
Hosting an Applet on a website which reads a textfile
By Bomber_Will in forum Java AppletsReplies: 3Last Post: 01-17-2009, 06:48 PM -
Simple example that reads a value from another class
By rizo in forum New To JavaReplies: 1Last Post: 11-12-2008, 03:43 PM -
Is there any utility that reads TimeZone data and display it in Human readable format
By Santhosh in forum Advanced JavaReplies: 0Last Post: 11-06-2007, 02:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks