The Artima Developer Community
Sponsored Link

Java Answers Forum
question about java.io

3 replies on 1 page. Most recent reply: Apr 7, 2003 12:17 AM by eti

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Ken

Posts: 7
Nickname: ken85
Registered: Apr, 2003

question about java.io Posted: Apr 2, 2003 3:19 AM
Reply to this message Reply
Advertisement
hello everyone i have two question while doing a java program and need some help from you guys.I have 2 questions

1)
public WriteFile {
public static void main(String args[]) {

ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("StudentDB"));

//a class i created to create studnent
Student studentA = new Student();
output.writeObject(studentA);
output.flush();
output.close();

}
}

//another class

public WriteFile2 {
public static void main(String args[]) {

ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("StudentDB"));

Student studentB = new Student();
output.writeObject(studentB);
output.flush();
output.close();
}}

Qu estion 1: i expect the result after running this two program is the file "studentDB" to carries the two object but instead, the 2nd program overrides the "studentDB".Can anyone teach me how can i write 2 objects in 2 seperate main method without overriding the file.



Question 2: after writing the "studentDB" file, i can read the Objects from it. But the problem is i wanted to get the Object, pass some methods to the object(eg . studentA.setName() ) and save it back "studentDB" do anyone here knows how to do it. I been trying this the whole day and can't get a solution


thank you


mayank

Posts: 1
Nickname: mayank
Registered: Apr, 2003

Re: question about java.io Posted: Apr 2, 2003 1:33 PM
Reply to this message Reply
Well, i don't have a good solution, but can suggest you a work around which will work fine, but is very expensive.

Write a method like this to Append a Student.

public void append(Student A)
{
try
{
File mainFile = new File("C:\\StudentDB");
File tempFile = new File("C:\\TempStudentDB");
ObjectInputStream input = new ObjectInputStream(new FileInputStream(mainFile));
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(tempFile));
try
{
while(true)
{
Student B = (Student)input.readObject();
output.writeObject(B);
}
}
catch(EOFException ex)
{
//ex.printStackTrace();
}
input.close();
output.writeObject(A);

output.flush();
output.close();
mainFile.delete();
tempFile.renameTo(mainFile);

}
catch(Exception ex)
{
ex.printStackTrace();
}
}

Similarly write a method to update a student.

Ken

Posts: 7
Nickname: ken85
Registered: Apr, 2003

Re: question about java.io Posted: Apr 3, 2003 5:44 AM
Reply to this message Reply
Thank you for your solution!! thats the only way i can do i guess . I will try to work out with this and if i encounter any problem , hope and i can get back to you in this forum for some guidance.Sorry, my english is bad =p
thankyou

eti

Posts: 3
Nickname: amuthan
Registered: Apr, 2003

Re: question about java.io Posted: Apr 7, 2003 12:17 AM
Reply to this message Reply
you can find another constructor for the class FileOutputStream(String filename,boolean appendflag)

instead of using

new ObjectOutputStream(new FileOutputStream(filename));

you can just replace

new ObjectOutputStream(new FileOutputStream(filename,true));

Hope this will solve your problem

Flat View: This topic has 3 replies on 1 page
Topic: Trouble with using recursive function on vector Previous Topic   Next Topic Topic: scroller doesn't funktion

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use