Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #13834 > unrolled thread
| Started by | bilsch <king621@comcast.net> |
|---|---|
| First post | 2012-04-23 16:58 -0700 |
| Last post | 2012-04-24 21:24 -0700 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
Need character output instead of numbers bilsch <king621@comcast.net> - 2012-04-23 16:58 -0700
Re: Need character output instead of numbers Joshua Cranmer <Pidgeot18@verizon.invalid> - 2012-04-23 19:31 -0500
Re: Need character output instead of numbers Roedy Green <see_website@mindprod.com.invalid> - 2012-04-24 09:03 -0700
Re: Need character output instead of numbers Lew <lewbloch@gmail.com> - 2012-04-24 13:21 -0700
Re: Need character output instead of numbers bilsch <king621@comcast.net> - 2012-04-24 21:24 -0700
| From | bilsch <king621@comcast.net> |
|---|---|
| Date | 2012-04-23 16:58 -0700 |
| Subject | Need character output instead of numbers |
| Message-ID | <jn4qae$5p1$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]
| From | Joshua Cranmer <Pidgeot18@verizon.invalid> |
|---|---|
| Date | 2012-04-23 19:31 -0500 |
| Message-ID | <jn4s8t$eme$1@dont-email.me> |
| In reply to | #13834 |
On 4/23/2012 6:58 PM, bilsch wrote: > Here is a simple example of reading a text file as integers and > displaying each with blank spaces in between. Also, don't multipost. It fragments the discussion. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-04-24 09:03 -0700 |
| Message-ID | <68jdp79qaa7409agltsp3t8r6ucbdrhl28@4ax.com> |
| In reply to | #13834 |
On Mon, 23 Apr 2012 16:58:05 -0700, bilsch <king621@comcast.net>
wrote, quoted or indirectly quoted someone who said :
>Here is a simple example of reading a text file as integers and
>displaying each with blank spaces in between.
It is hard to know where you are confused.
So I will just tell you some things:
public int read()
throws IOException
Reads the next byte of data from this input stream. The value byte is
returned as an int in the range 0 to 255. If no byte is available
because the end of the stream has been reached, the value -1 is
returned. This method blocks until input data is available, the end of
the stream is detected, or an exception is thrown.
Note how read() reads one unsigned byte but returns an int so that is
can use -1 as an eof marker.
See http://mindprod.com/applet/fileio.html
It will generate you code for pretty well any i/o purpose. Just tick
off the boxes what you want.
If you want to read binary 16 bit ints, you would use a
DataInputStream.readShort or readChar
To read 32 bit ints you would use readInt
>I looked at the input file with a hex editor and the ASCII chars are all
>stored as bytes - not integers.
That is to be expected. ASCII files are always just a series of
bytes. What did you hope to see?
>QUESTION 2) What do I need to do to get ASCII characters as output
instead of numbers?
An ASCII file is string of bytes. You can look at them as chars with a
text editor decimal bytes with a java progam like yours or hex bytes
with a hex editor. They are just bits.
If you are new to programming you may not have scaled the hump of
understanding the difference between a binary number representation
and a string of chars representation for the same value.
See http://mindprod.com/jgloss/binary.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
When you were a child, if you did your own experiment
to see if it was better to put to cocoa into your cup first
or the hot milk first, then you likely have the programmer gene..
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-04-24 13:21 -0700 |
| Message-ID | <3342739.918.1335298860476.JavaMail.geo-discussion-forums@pbac10> |
| In reply to | #13834 |
bilsch wrote:
> Here is a simple example of reading a text file as integers and
Strictly speaking, you can only read a text file as text, then convert (some of) that text to an integer.
> 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.
Right.
> 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();
Nope. Read the Javadocs for that method, and you can clearly see that the return type is 'int'.
> 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?
First of all, your question is not valid. Nothing is read from any 'InputStream' as a 'char'. That only happens with 'Reader' types. 'InputStream#read()' returns values from zero to 255, inclusive, corresponding to the bytes in the file, until EOF when it returns -1.
<http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read()>
Read the Javadocs. Also the part of Roedy's response that explains: "Note how read() reads one unsigned byte but returns an int so that is can use -1 as an eof marker." That is a restatement of what's in the Javadocs, so really you should read the Javadocs.
> QUESTION 2) What do I need to do to get ASCII characters as output
> instead of numbers?
ASCII characters *are* numbers.
What do you really want to do?
I'd do something like:
BufferedReader reader = new BufferedReader(new FileReader("NVRAM.TXT"));
and use its methods to return 'String' values directly, but if I were counting bytes read, as you seem to want, I'd do basically what you already did.
You can also convert byte arrays to 'String' values if you use other API methods and are very, very rigorous about character encoding. Are you?
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | bilsch <king621@comcast.net> |
|---|---|
| Date | 2012-04-24 21:24 -0700 |
| Message-ID | <jn7ua3$a8o$1@dont-email.me> |
| In reply to | #13865 |
Thanks everyone for your help. I am new to Java and haven't done any programming in many years.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web