Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1771 > unrolled thread

Need output of characters instead of numbers

Started bybilsch <king621@comcast.net>
First post2012-04-23 16:52 -0700
Last post2012-04-24 13:37 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.help


Contents

  Need output of characters instead of numbers bilsch <king621@comcast.net> - 2012-04-23 16:52 -0700
    Re: Need output of characters instead of numbers Joshua Cranmer <Pidgeot18@verizon.invalid> - 2012-04-23 19:30 -0500
    Re: Need output of characters instead of numbers Lew <lewbloch@gmail.com> - 2012-04-24 13:37 -0700

#1771 — Need output of characters instead of numbers

Frombilsch <king621@comcast.net>
Date2012-04-23 16:52 -0700
SubjectNeed output of characters instead of numbers
Message-ID<jn4pv3$41i$1@dont-email.me>
Here is a simple example of reading a text file as integers and 
displaying each with blank spaces in between.

> import java.io.*;
>
> public class Nvj3 {
>
>     public static void main(String[] args){
>         try {
>         FileInputStream file = new FileInputStream("NVRAM.TXT");
>         boolean eof = false;
>         int count = 0;
>         while (!eof) {
>             int input = file.read();
>             System.out.print(input + " ");
>             if (input == -1)
>                 eof = true;
>             else
>                 count++;
>
>
>         }
>         System.out.println("Bytes read : " + count);
>
>     } catch (IOException e){
>         System.out.println("Could not read file");
>     }
>
>     }
> }
>

OUTPUT is positive integers less than 128  (ASCII):
78 86 82 65 77 44 32 86 101 114 115 105 111 110 32 49 46 50 48 13 10

78 111 110 45 86 111 108 97 116 105 108 101 32 82 65 77 32 83 121 115

I looked at the input file with a hex editor and the ASCII chars are all 
stored as bytes - not integers.

I HAVE TWO QUESTIONS: the statement: int input = file.read();
reads the file as a bunch of integer type values but the output is in 
character type.  It seems like the correct input statement would be:
	char input = file.read();
I tried that and it is an error.  QUESTION 1): Can someone explain why 
the file is read as char type but the statement says int type?

QUESTION 2) What do I need to do to get ASCII characters as output 
instead of numbers?

TIA   Bill (in Seattle, WA, USA)

[toc] | [next] | [standalone]


#1772

FromJoshua Cranmer <Pidgeot18@verizon.invalid>
Date2012-04-23 19:30 -0500
Message-ID<jn4s6s$ekj$1@dont-email.me>
In reply to#1771
On 4/23/2012 6:52 PM, bilsch wrote:
> I HAVE TWO QUESTIONS: the statement: int input = file.read();
> reads the file as a bunch of integer type values but the output is in
> character type. It seems like the correct input statement would be:
> char input = file.read();
> I tried that and it is an error. QUESTION 1): Can someone explain why
> the file is read as char type but the statement says int type?

The file is not read as a char type. A Java char is a UTF-16 codepoint. 
InputStreams treat their inputs as a stream of byte elements. However, 
it is also necessary for the read() method to return a sigil that 
indicates that the end of the file has occurred, which means it cannot 
use the byte type since the sigil would override a valid character. The 
type returned is instead widened to an int, with the values 0-255 
indicating a valid octet in the file and the value -1 indicating that 
the end-of-the-file has been reached.

> QUESTION 2) What do I need to do to get ASCII characters as output
> instead of numbers?

The best way is to not use an InputStream in the first place but instead 
use a FileReader.


-- 
Beware of bugs in the above code; I have only proved it correct, not 
tried it. -- Donald E. Knuth

[toc] | [prev] | [next] | [standalone]


#1775

FromLew <lewbloch@gmail.com>
Date2012-04-24 13:37 -0700
Message-ID<9538222.916.1335299865375.JavaMail.geo-discussion-forums@pbbox1>
In reply to#1771
bilsch wrote:
> Here is a simple example of reading a text file as integers and 
> displaying each with blank spaces in between.
> 

Asked and answered in your other multipost. 

-- 
Lew

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web