java and file handling

writing and reading in files

Page 1 of 1

4 Replies - 10953 Views - Last Post: 03 May 2006 - 10:37 AM Rate Topic: -----

#1 smallika  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-May 06

java and file handling

Posted 02 May 2006 - 05:25 AM

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++?
Is This A Good Question/Topic? 0
  • +

Replies To: java and file handling

#2 Favouriteegent  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 02-May 06

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
Was This Post Helpful? 0
  • +
  • -

#3 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

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.
Was This Post Helpful? 0
  • +
  • -

#4 Favouriteegent  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 02-May 06

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)
Was This Post Helpful? 0
  • +
  • -

#5 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

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.

This post has been edited by William_Wilson: 03 May 2006 - 10:39 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1