Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

read file as object

Thread view: 
Carramba - 27 Jan 2006 20:52 GMT
hello!

Iam wondering if there is a way to read content of the file as object and  
put it in arraylist.
then have possibility to cast elements of object to char, or other  
primitiv data type.

Thanx in advance
________________________
zero - 27 Jan 2006 21:03 GMT
> hello!
>
> Iam wondering if there is a way to read content of the file as object
> and  put it in arraylist.
> then have possibility to cast elements of object to char, or other  
> primitiv data type.

It all depends on the contents of the file.  If you used an
ObjectOutputStream to write Character objects to the file, you can read
them back with an ObjectInputStream, and get the char value with
Character:charValue().
Carramba - 27 Jan 2006 21:14 GMT
but if not? I mean if file was created some other way,
can one read byts as object and cat it to char.
I want to make abstract io class for the application, so I don't have to  
care then I read file what it contains.
but I can extend it to cast byts (for ex) to char or int or what ever

>> hello!
>>
[quoted text clipped - 7 lines]
> them back with an ObjectInputStream, and get the char value with
> Character:charValue().

Signature

Thanx in advance
________________________

Oliver Wong - 27 Jan 2006 21:20 GMT
> but if not? I mean if file was created some other way,
> can one read byts as object and cat it to char.
> I want to make abstract io class for the application, so I don't have to
> care then I read file what it contains.
> but I can extend it to cast byts (for ex) to char or int or what ever

   One could read the file directly as a sequence of chars, rather than
reading it as objects and then casting to char. See
http://java.sun.com/j2se/1.5.0/docs/api/java/io/Reader.html#read()

   One could also read the file directly as a sequence of bytes. See
http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileInputStream.html#read()

   - Oliver
Carramba - 27 Jan 2006 21:41 GMT
well, thanx for pointing it out,
but my question can I read bytes to arraylist, and later or
if I wanto cast them to char and wrap as objects?
and how do I cast & wrap in that case? shoud one take 2 byts at the time?  
for char in this case?

>> but if not? I mean if file was created some other way,
>> can one read byts as object and cat it to char.
[quoted text clipped - 10 lines]
>
>     - Oliver

Signature

Thanx in advance
________________________

Oliver Wong - 27 Jan 2006 21:52 GMT
> well, thanx for pointing it out,
> but my question can I read bytes to arraylist, and later or
> if I wanto cast them to char and wrap as objects?
> and how do I cast & wrap in that case? shoud one take 2 byts at the time?
> for char in this case?

   I'm not sure I understand your questions, so I'm going to make some
statements, and hopefully these statements will contain the information you
want.

   You can read the bytes one at a time, and put them into an ArrayList of
Byte objects, or you could read the bytes into an array of bytes (e.g.
byte[]).

   Given a Byte object, you can get its corresponding byte value. Given a
byte, you can create a corresponding Byte object.

   Given a Character object, you can get its corresponding char value.
Given a char, you can create a corresponding Character object.

   To convert between bytes and chars (or Bytes and Characters), you need
to use an encoding, e.g. UTF-8, ASCII, etc. Not all encodings map 2 bytes to
1 character.

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.