Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default 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
    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();
    			}
    		}
    	}
    }
    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?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default Re: FileReader reads all data as int

    Java Code:
               int line;
    	   while ((line = original.read()) != -1) {
    Your code is reading the bytes of the file into an int variable.

    getting the expected result
    The read method you are using returns a int value.

    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.

  3. #3
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: FileReader reads all data as int

    Quote Originally Posted by Norm View Post
    Java Code:
               int line;
    	   while ((line = original.read()) != -1) {
    Your code is reading the bytes of the file into an int variable.


    The read method you are using returns a int value.

    What do you want to happen when you read the file?
    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?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default 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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,438
    Rep Power
    18

    Default 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.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,438
    Rep Power
    18

    Default Re: FileReader reads all data as int

    Gah!
    Ninja'd!
    :)

  7. #7
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: FileReader reads all data as int

    Quote Originally Posted by Norm View Post
    If you want to read each line of the file into a String, look at wrapping the FileReader in a BufferedReader.
    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.

  8. #8
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: FileReader reads all data as int

    Quote Originally Posted by Tolls View Post
    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.
    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.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default Re: FileReader reads all data as int

    I'd like to know if it's possible to read byte by byte
    That is what the code in your first post did.

    Do you understand that the value of '1' is not 1?

  10. #10
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: FileReader reads all data as int

    Quote Originally Posted by Norm View Post
    That is what the code in your first post did.
    Yes, however when I print out that code the console output is a bunch of integers. I'd like to be able to retain the characters as characters when I print them out and the integers as integers. Thanks.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default 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?

  12. #12
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: FileReader reads all data as int

    Quote Originally Posted by Norm View Post
    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?
    Yes, from C/C++ I learned that it's some ASCII table that converts it right?

    If I cast to char, wouldn't it change all the numbers to char? Even if the file contained a legit number, it would convert it to char?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default 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

  14. #14
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: FileReader reads all data as int

    Quote Originally Posted by Norm View Post
    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
    Thanks, I'm guessing that is casting only English letters to char? Is this the only way to approach this problem of printing a file's content character by character?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default 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.

  16. #16
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: FileReader reads all data as int

    Quote Originally Posted by Norm View Post
    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.
    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 ...

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default 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.

  18. #18
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: FileReader reads all data as int

    Quote Originally Posted by Norm View Post
    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.
    OK, then how is BufferedReader able to determine what is a letter and what is a number and what is a symbol? Thanks.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default 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.

  20. #20
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: FileReader reads all data as int

    Quote Originally Posted by Norm View Post
    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.
    Oh OK, so is the best way for me to compare character by character from 2 files would be to read them in as BufferedReader and then compare the resulting strings character for character?

Page 1 of 2 12 LastLast

Similar Threads

  1. Write a Program that reads two times in military format
    By HPcompaq256 in forum New To Java
    Replies: 10
    Last Post: 02-26-2010, 04:38 AM
  2. add FileReader to GUI
    By VinTiger in forum New To Java
    Replies: 8
    Last Post: 05-11-2009, 12:23 AM
  3. Hosting an Applet on a website which reads a textfile
    By Bomber_Will in forum Java Applets
    Replies: 3
    Last Post: 01-17-2009, 06:48 PM
  4. Replies: 1
    Last Post: 11-12-2008, 03:43 PM
  5. Replies: 0
    Last Post: 11-06-2007, 02:20 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
  •