participate


Java Programming [Archive] - File IO. Every byte from 128-159 comes out 63
<<   Back to Forum  |   Give us Feedback
This topic has 5 replies on 1 page.
gorregisguy
Posts:6
Registered: 10/26/04
File IO. Every byte from 128-159 comes out 63   
Mar 30, 2005 2:19 PM

 
I was just trying out some very basic comprassion algorithms(hardly comprassed really) and I noaticed that whenever I input 128, the byte not the int, when I read that byte it shows up as 63. Every single time. Im using Java 5 and JCreator 3.50.011.
Here's the code I used to test.
import java.io.*;
 
public class Test 
{
    public static void main(String[] args) 
    {
    	PrintWriter out;
    	BufferedReader in;
    	try
    	{
    		out = new PrintWriter(new FileOutputStream(new File("hello.txt")));
    		out.write(128);
    		out.close();
    	}catch(Exception e)
    	{
    		System.err.println(e);
    	}
    	try
    	{
    		in = new BufferedReader(new FileReader(new File("hello.txt")));
    		System.out.println(in.read());
    	}catch(Exception e)
    	{
    		System.err.println(e);
    	}
    }
}

When I added a for loop to that I got 63 for every number from 128-159.
Does anyone know why this is happning? Any help is greatly appricated.

[gorregisguy]
 
javaxodus
Posts:303
Registered: 3/7/05
Re: File IO. Every byte from 128-159 comes out 63   
Mar 30, 2005 2:30 PM (reply 1 of 5)  (In reply to original post )

 
I believe this is happening because you are outputting things that are larger than a byte. The byte integer values only go from -128 to 127 IIRC.
 
gorregisguy
Posts:6
Registered: 10/26/04
Re: File IO. Every byte from 128-159 comes out 63   
Mar 30, 2005 2:36 PM (reply 2 of 5)  (In reply to #1 )

 
Thanks for the repley [b]javaxodus[\b]. Unfortenetly that wasn't the problem for two reasons. First it outputs all the numbers of 159 correctly. Second I tested it using -127 as the starting [b]i[\b] position and everything below 0 came out as 63 as well. Thanks for tring.

[gorregisguy]
 
mlk
Posts:10,971
Registered: 8/15/01
Re: File IO. Every byte from 128-159 comes out 63   
Mar 30, 2005 2:53 PM (reply 3 of 5)  (In reply to #2 )

 
Your writing binary data with Readers (which are for character streams).

Use InputStream and OutputStream for writing binary data, use Readers for writing strings.
 
MLRon
Posts:7,758
Registered: 2/16/04
Re: File IO. Every byte from 128-159 comes out 63   
Mar 30, 2005 2:55 PM (reply 4 of 5)  (In reply to #2 )

 
http://encyclopedia.laborlawtalk.com/Table_of_Unicode_characters,_128_to_999

Since the first 256 code points of Unicode are based on ISO 8859-1, Unicode characters 128 to 159 are unused characters, corresponding to the upper control character set of ISO 8859-1. Even if characters in the range 128 to 159 display something sensible on your browser, they cannot be relied upon to display the same thing ? or anything at all ? on any other browser, so they should never be used.

Did you look at hello.txt? All 128-159 probably come out as question marks (128 did for me), which is Unicode 63.
 
gorregisguy
Posts:6
Registered: 10/26/04
Re: File IO. Every byte from 128-159 comes out 63   
Mar 30, 2005 3:05 PM (reply 5 of 5)  (In reply to #4 )

 
Thanks for the quick replies. I changed the two streams to FileInputStream and FileOutputStream and it worked perfectly. Thanks again.

[gorregisguy]
 
This topic has 5 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums