Re: How can I use class FileInputStream as a reverse of FileOutputStream?

From: Jean-Benoit MORLA (jbmorla_at_tiscali.fr)
Date: 11/05/04


Date: 5 Nov 2004 02:11:09 -0800

Thomas Weidenfeller <nobody@ericsson.invalid> wrote in message news:<cmdco1$2fm$1@newstree.wise.edt.ericsson.se>...
> Jean-Benoit MORLA wrote:
> > So I searched the Sun Java 1.5 site for the io package and
> > I found the FileInputStream class.
> > However it states that "FileInputStream is meant for reading streams
> > of raw bytes such as image data."
> > It is advised to use FileReader instead.
> > So what should I do:
> > - try method read( byte[] b ) with b the length of the array of bytes
> > ( but I don't see how to set b for each new line of the .txt file )
> > - Stick to FileReader and FileWriter
>
> Java indeed makes a distinction between characters and bytes (as
> opposite to what is e.g. common in C). Java also handles different
> encodings (an encoding specifies how characters are represented as
> bytes). When you use the I/O classes intended for reading/writing text,
> the conversion (decoding/encoding) is done by Java for you, which is
> usually a good thing.
>
> So, if
>
> * you want to read text data and work with it as text, use a Reader of
> some kind.
>
> * you want to write text, and make sure it is indeed ending up in some
> text encoding in the file, use a Writer of some kind.
>
> * you want to read binary data, and don't want the data to get
> interpreted as some text encoding, use some kind of InputStream.
>
> * you want to write binary data, and want to make sure that it is not
> changed due to some text encoding, use some kind of OutputStream.
>
> You can read text files as binary data. This is only useful in some
> special cases, and not a good idea when you really want to deal with
> text. You can also create text files using the binary writing classes.
> But again, this is only useful in very special cases.
>
> /Thomas

>>> Thomas,
>>> Many thanks for your clear reply
>>> I think it's time I look up for the Unicode-dealing classes
>>> Have a nice day



Relevant Pages

  • Re: How do I write binary data to a file with javascript & WSH?
    ... I guess I'm not thinking in terms of the correct encoding. ... binary data to a file via a javascript and Windows Script Host ... is to convert the binary data to text via String.fromCharCode ... characters but it accepts some and not others. ...
    (comp.lang.javascript)
  • Re: How can I use class FileInputStream as a reverse of FileOutputStream?
    ... > I found the FileInputStream class. ... Java indeed makes a distinction between characters and bytes (as ... text encoding in the file, use a Writer of some kind. ... * you want to write binary data, and want to make sure that it is not ...
    (comp.lang.java.help)
  • Re: object to a byte[]
    ... I just picked the first encoding on the list :-) ... space, since two bytes (or more, depending on the MBCS binary encoding) ... > If what you are asking is how to store binary data in a text DB field, ... > Be warned, though, if you are not saving text characters make absolutely ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Encoding in Textboxes
    ... they're UTF-16 (so characters not in the BMP are ... encoding for the most part - encodings are really needed when ... converting text data to/from binary data. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Encoding of primitives for binary serialization
    ... Also I need to handle serialization/de-serialization across java and c++ i.e. serialization from java and de-serialization in c++ and vice-versa. ... For this I need to decide an encoding for primitive data types which is independent of language and platform. ... You can do signed and unsigned, but be aware that in java, which has no native unsigned types, you'll need to use the next bigger type to hold unsigneds, eg an unsigned short will need an int to hold. ... UTF-16 will let you encode all characters in two bytes each, and is simple to do. ...
    (comp.lang.java.programmer)