• 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

Object serialization writeChars()

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, Lucky!

Can you show us the code that causes the exception?


Bu.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic