This discussion is archived
6 Replies Latest reply: Dec 19, 2012 5:21 AM by 980587 RSS

PrintStream Problem - Bug?

980587 Newbie
Currently Being Moderated
Hey, i tried the following for getting a file with with bytes from 0x00 to 0xFF. In the file is a 0x3F written, where the bytes from 0x80 until 0x9F should be.

Do you think this is a bug or do I something wrong?

Regards,

CoLa

<source>


import java.io.*;

public class ExpressionForTxt {
     


          public static void main(String[] args) throws IOException {
               

               try{
               
               PrintStream target = new PrintStream(new FileOutputStream(new File("outputformat_regexpr.txt")));
               FileOutputStream target2 = new FileOutputStream(new File("outputformat_regexpr2.txt"));
     
                    

               
               for(int i = 0;i<=255;i++){
                    

                    System.out.println((char) i);
          
                    for(int j = 0;j<1;j++){

                    target2.write(i);                    //Everything fine
                    target.append((char) i);          //Bytes 0x80 until 0x9F written as 0x3F
                    


                    }

               }
               
               
               }
               catch(Exception e){
                    System.out.println(e);
               }
               
          }
          

     }


</source>

Edited by: 977584 on 22.12.2012 06:25
-Real Name
+Pseudonym                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
  • 1. Re: PrintStream Problem - Bug?
    jtahlborn Expert
    Currently Being Moderated
    are you trying to write bytes or characters ? you question says "bytes", but your code says "characters" (and they are 2 different things).

    also, your code is writing 2 files. to which is your question referring?
  • 2. Re: PrintStream Problem - Bug?
    Kayaman Guru
    Currently Being Moderated
    977584 wrote:
    Hey, i tried the following for getting a file with with bytes from 0x00 to 0xFF. In the file is a 0x3F written, where the bytes from 0x80 until 0x9F should be.
    Do you think this is a bug or do I something wrong?
    How likely do you think it is that a beginner would discover a bug in something like PrintStream?
    0x3F is the question mark in ASCII. Question marks are inserted when the character can't be represented in the specified encoding. Since you didn't specify an encoding, it's defaulting to something which isn't working. Always specify and encoding preferably UTF-8 or similar.
    Use OutputStreamWriter to specify the encoding you wish to use when writing to the file (such as ISO-8859-1) and see what happens.
    <source>
    The correct formatting tag is
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 3. Re: PrintStream Problem - Bug?
    980587 Newbie
    Currently Being Moderated
    Thanks!
    I didn't know that OutputStream was doing a re-encoding of my data. Coming from hardware programming with C/C++, it isn't obvious, that functions do this high level stuff ;P

    Regards,
    CoLa
  • 4. Re: PrintStream Problem - Bug?
    gimbal2 Guru
    Currently Being Moderated
    977584 wrote:
    Thanks!
    I didn't know that OutputStream was doing a re-encoding of my data. Coming from hardware programming with C/C++, it isn't obvious, that functions do this high level stuff ;P
    You couldn't make that leap when you decided to start using a high level language? It isn't productive to keep yourself in C++ land, I would read around and take away some more wrong assumptions you are likely making.

    Also the javadocs generally mention what JDK classes do under the hood. Whatever IDE you're using, it may pay off to figure out how to properly make it reference javadoc documentation.
  • 5. Re: PrintStream Problem - Bug?
    Kayaman Guru
    Currently Being Moderated
    977584 wrote:
    Thanks!
    I didn't know that OutputStream was doing a re-encoding of my data.
    It's not actually OutputStream, since that wouldn't re-encode anything, just write bytes.
    It's the PrintStream that converts characters to bytes that causes it.
    From the javadoc: "All characters printed by a PrintStream are converted into bytes using the platform's default character encoding. The PrintWriter class should be used in situations that require writing characters rather than bytes.".

    So PrintStream is one of those classes that shouldn't really be used, but there's no way to know that unless...you know.
  • 6. Re: PrintStream Problem - Bug?
    980587 Newbie
    Currently Being Moderated
    Sorry, I meant PrintStream. As you have seen, I used OutputStream instead, and everything was fine, because no encoding was done!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points