Path: csiph.com!usenet.pasdenom.info!goblin1!goblin.stu.neva.ru!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: Need character output instead of numbers Date: Tue, 24 Apr 2012 13:21:00 -0700 (PDT) Organization: http://groups.google.com Lines: 89 Message-ID: <3342739.918.1335298860476.JavaMail.geo-discussion-forums@pbac10> References: NNTP-Posting-Host: 69.28.149.29 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1335298861 12416 127.0.0.1 (24 Apr 2012 20:21:01 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 24 Apr 2012 20:21:01 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=69.28.149.29; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 Xref: csiph.com comp.lang.java.programmer:13865 bilsch wrote: > Here is a simple example of reading a text file as integers and=20 Strictly speaking, you can only read a text file as text, then convert (som= e of) that text to an integer. > displaying each with blank spaces in between. >=20 > > import java.io.*; > > > > public class Nvj3 { > > > > public static void main(String[] args){ > > try { > > FileInputStream file =3D new FileInputStream("NVRAM.TXT"); > > boolean eof =3D false; > > int count =3D 0; > > while (!eof) { > > int input =3D file.read(); > > System.out.print(input + " "); > > if (input =3D=3D -1) > > eof =3D true; > > else > > count++; > > > > > > } > > System.out.println("Bytes read : " + count); > > > > } catch (IOException e){ > > System.out.println("Could not read file"); > > } > > > > } > > } > > >=20 > 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 >=20 > I looked at the input file with a hex editor and the ASCII chars are all= =20 > stored as bytes - not integers. Right. > I HAVE TWO QUESTIONS: the statement: int input =3D file.read(); > reads the file as a bunch of integer type values but the output is in=20 > character type. It seems like the correct input statement would be: > char input =3D file.read(); Nope. Read the Javadocs for that method, and you can clearly see that the r= eturn type is 'int'. > I tried that and it is an error. QUESTION 1): Can someone explain why=20 > 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 'InputSt= ream' 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. Read the Javadocs. Also the part of Roedy's response that explains: "Note h= ow 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 real= ly you should read the Javadocs. > QUESTION 2) What do I need to do to get ASCII characters as output=20 > instead of numbers? ASCII characters *are* numbers. What do you really want to do? I'd do something like: BufferedReader reader =3D new BufferedReader(new FileReader("NVRAM.TXT"));= =20 and use its methods to return 'String' values directly, but if I were count= ing 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 me= thods and are very, very rigorous about character encoding. Are you? --=20 Lew