Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: FileReader.read() reads garbage

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default FileReader.read() reads garbage

    Hi,

    I have a problem with the FileReader class. I want to read and copy a file and skip certain bytes. However, when I have a FileReader fr and I have it read(), at a very certain point it reads something else entirely, and subsequently writes that garbage into the new file. It only happens with a certain file though. Here is example code and the file in question:

    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
     
     
    public class Trunc {
     
    	/**
    	 * @param args
    	 * @throws Exception 
    	 */
    	public static void main(String[] args) throws Exception {
    		String in = "in.txt";
    		String out = "out.txt";
     
    		File inFile = new File(in);
    		File outFile = new File(out);
     
    		FileReader fr = new FileReader(inFile);
    		FileWriter fw = new FileWriter(outFile);
     
    		while(fr.ready()) {
                                                    int temp = fr.read();
    			fw.write(temp);
                                    }
     
    		fr.close();
    		fw.close();
                     }
    }

    That's really just a fancy way of copying a file byte for byte so I can edit or leave out bytes as they are read.

    The problem is this:

    The in.txt that I have appended has the value 0x90 at address 0x1E5. However, when I run the above code on in.txt, the out.txt suddenly has the value 0x3F at the same address, and a System.out.print tells me that the read() function has read the nonsense value 0xFFFD at that position!

    The same thing happens several times in the file, with different in-values between 0x80 and 0x9D, maybe more, but always replacing the original value with 0x3F in the out file.

    It also happens with 1-byte files that contain, for example, the value 0x90 as the only byte. It gets replaced to 0x3f in the out file.

    What is the problem here and how can I fix that? Remember that I don't just want to copy the file wholesome, I want to edit/skip certain bytes before writing.
    Attached Files Attached Files


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: FileReader.read() reads garbage

    Take a look at the API for FileReader. In particular note the following line:
    "FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream."

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: FileReader.read() reads garbage

    Also consider the character encoding. 0x90 is not in your default character set.
    The FileReader API doc says:
    constructors of this class assume that the default character encoding
    The 0x90 is converted to a ? (0x3F)

    To read other character sets, look at setting the default character set.
    See the Charset class.

  4. #4
    Junior Member
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: FileReader.read() reads garbage

    Ok thanks, will try that. Didn't read FileReader properly because I skipped to Reader for the inherited methods.

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: FileReader.read() reads garbage

    FileInputStream indeed solved the problem. Thanks again.

Similar Threads

  1. [SOLVED] difference between fileReader and bufferReader
    By shadihrr in forum Java Theory & Questions
    Replies: 6
    Last Post: June 8th, 2010, 01:26 AM
  2. FileReader need assistance
    By tazjaime in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 8th, 2009, 01:12 AM
  3. Java Garbage Collection and destructors
    By riddhik84 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 1st, 2009, 09:06 AM
  4. Garbage Collection?
    By kalees in forum Collections and Generics
    Replies: 6
    Last Post: September 23rd, 2009, 03:07 AM
  5. A program that reads in secounds, and prints out hours minutes
    By CYKO in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2009, 10:42 PM