I was trying to read and write objects to files. But I could not do this with multiple objects. Also, I want to know how we can read, write or modify objects in files at random - the way we do in C++?
java and file handlingwriting and reading in files
Page 1 of 1
4 Replies - 10022 Views - Last Post: 03 May 2006 - 10:37 AM
Replies To: java and file handling
#2
Re: java and file handling
Posted 02 May 2006 - 10:26 AM
// Create an ObjectOutputStream (import java.io.*);
'// Tie it to an OutputStream and use the writeObject method
try
{
ServerSocket server = new ServerSocket(9999, 1);
Socket current = server.accept();
System.out.println("client " + current.toString());
ObjectOutputStream oos = new
ObjectOutputStream(current.getOutputStream());
I doubt whether you'll be able to modify them on the stream though,
Paul
'// Tie it to an OutputStream and use the writeObject method
try
{
ServerSocket server = new ServerSocket(9999, 1);
Socket current = server.accept();
System.out.println("client " + current.toString());
ObjectOutputStream oos = new
ObjectOutputStream(current.getOutputStream());
I doubt whether you'll be able to modify them on the stream though,
Paul
#3
Re: java and file handling
Posted 02 May 2006 - 11:25 AM
Java does not like to allow such modifications, but i'm sure with MUCH inefficiency and trouble it could be possible though... For all the time it takes to load objects in with java, it would be just as efficient to load it then modify it.
You can always use a Vector to save multiple objects.
You can always use a Vector to save multiple objects.
#4
Re: java and file handling
Posted 03 May 2006 - 10:13 AM
Hi,
tried the following without success - once you write Objects to the stream that is it...
The output of the following is:
Fred
Bob
FileOutputStream fos = new FileOutputStream("c:/out.dat");
ObjectOutputStream out = new ObjectOutputStream(fos);
String fred = "Fred";
String bob = "Bob";
out.writeObject((Object)fred);
out.writeObject((Object)bob);
fred = "Frederick";
bob = "Robert";
out.flush();
fos.close();
FileInputStream fis = new FileInputStream("c:/out.dat");
ObjectInputStream in = new ObjectInputStream(fis);
System.out.println((String)in.readObject());
System.out.println((String)in.readObject());
fis.close();
cheers,
Fav (The Vector approach will only work in 5.1.x if you cast them all to an Object type) and the Vector is a template of Objects)
tried the following without success - once you write Objects to the stream that is it...
The output of the following is:
Fred
Bob
FileOutputStream fos = new FileOutputStream("c:/out.dat");
ObjectOutputStream out = new ObjectOutputStream(fos);
String fred = "Fred";
String bob = "Bob";
out.writeObject((Object)fred);
out.writeObject((Object)bob);
fred = "Frederick";
bob = "Robert";
out.flush();
fos.close();
FileInputStream fis = new FileInputStream("c:/out.dat");
ObjectInputStream in = new ObjectInputStream(fis);
System.out.println((String)in.readObject());
System.out.println((String)in.readObject());
fis.close();
cheers,
Fav (The Vector approach will only work in 5.1.x if you cast them all to an Object type) and the Vector is a template of Objects)
#5
Re: java and file handling
Posted 03 May 2006 - 10:37 AM
You don't have to declare the type as object, as Strings are objects.
try this for File Input
and this for File Output
*I wrote them both so i know they work
If you will be saving strings, a Vector is a great choice, otherwise, yes you would have to convert each primative to it's wrapper object equivalent.
try this for File Input
and this for File Output
*I wrote them both so i know they work
If you will be saving strings, a Vector is a great choice, otherwise, yes you would have to convert each primative to it's wrapper object equivalent.
This post has been edited by William_Wilson: 03 May 2006 - 10:39 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|