Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Liutauras Vilda
Paul Clapham
Sheriffs:
paul wheaton
Tim Cooke
Henry Wong
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Piet Souris
Bartenders:
Mike London
Forum:
Programmer Certification (OCPJP)
Object serialization writeChars()
Lucky J Verma
Ranch Hand
Posts: 278
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
In serialization ,
when i write a
String
using writeObject() ,it works
but when i use writeChars() ,which is also for wrting a String,
IOException comes.but other writeXXX() like writeLong() works.
where can be the problem .
Burkhard Hassel
Ranch Hand
Posts: 1274
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Howdy, Lucky!
Can you show us the code that causes the exception?
Bu.
all events occur in real time
Lucky J Verma
Ranch Hand
Posts: 278
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
this is my program
Here writeChars() to write a string does cause an exception but with writeObject it works.
why,i dont get.
<code>
import java.io.*;
public class ObjectserializationDemo {
public static void main(String args[])
{
ObjectserializationDemo osd=new ObjectserializationDemo();
osd.writeData();
osd.readData();
}
//---------------------------------------------------------------
void readData()
{
try{
FileInputStream fis=new FileInputStream("fileobject.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
long l1;
System.out.println("Reading.... ");
String s1=(String)ois.readObject();
//Object s1=ois.readObject();
System.out.println(s1);
String sa[]=(String[])ois.readObject();
for(String t:sa){System.out.println(t);}
l1=ois.readLong();
System.out.println("long is "+l1);
int[] a1=(int[])ois.readObject();
for(int i:a1){System.out.println(i);}
ois.close(); fis.close();
}catch(FileNotFoundException e){System.out.println("File not found " +e);}
catch(IOException e){System.out.println("IO problem " +e );e.printStackTrace();}
catch(ClassNotFoundException e){System.out.println("Class not found " +e);}
}
//---------------------------------------------------------------
void writeData()
{
try{
FileOutputStream fos=new FileOutputStream("fileobject.ser");
ObjectOutputStream oos=new ObjectOutputStream(fos);
String str1="this is my first string to be serialized";
String str2[]={"this"," is "," string "," array ",};
long num=1979;
int[] arr1={26,11,1979};
//oos.writeChars(str1); //-----------------this doesntnt work
oos.writeObject(str1); //oos.writeC
oos.writeObject(str2);
oos.writeLong(num);
oos.writeObject(arr1);
oos.close();fos.close();
}
catch(FileNotFoundException e){System.out.println("File not found " +e);}
catch(IOException e){System.out.println("IO problem " +e);}
}
//------------------------------------------------------------
}
</code>
Burkhard Hassel
Ranch Hand
Posts: 1274
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Howdy Lucky,
thanks for the code. The writing is not the problem, but the reading.
Earnest knows practically everything, so please have a look at this
thread
from
Java
in General (intermediate):
https://coderanch.com/forums/
Yours,
Bu.
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
How to write a string in a file?
Serialization and Inner Classes
Counterpart to ObjecOutputStream#writeChars?
Writing Readable Text to a File Using Streams
writeChars & writeUTF
More...