Discussions
Categories
Groups
Advertisement
Child Item
Home
Topics
Technology & Internet
Software & Web Development
Development
Java
darren85
hi ive the following code to save data to a txt file. but its not saving right, can any1 spot what im doing wrong? grid2 is the button i click and the numfld are just textboxs from which i want to take the values entered by user and save them to bookstore.txt.
// save to text file here
if(e.getSource() == grid2Btn)
{
FileOutputStream outputBookStore;
ObjectOutputStream objSaveBookStore;
try
{
outputBookStore = new FileOutputStream("BookStore.txt", true);
objSaveBookStore = new ObjectOutputStream(outputBookStore);
BookList BkCurrent = new BookList(num1Fld.getText(), num2Fld.getText(), num4Fld.getText(), num5Fld.getText());
objSaveBookStore.writeObject(BkCurrent);
objSaveBookStore.flush();
num1Fld.setText("");
num2Fld.setText("");
num4Fld.setText("");
num5Fld.setText("");
JOptionPane.showMessageDialog(null, "New Book Details Saved");
ButtonPanel.add(grid2Btn);
outputBookStore.close();
objSaveBookStore.close();
}
catch(Exception error)
{
System.err.println("Error Opening File");
}
}
Find more posts tagged with
Quick Links
All Categories
Recent Posts
Activity
Unanswered
Groups
Best Of
Advertisement
Advertisement
Comments
OfflerCrocGod
What do you mean "not saving right"? You do realise that you are saving Objects here not textual data so if you open it up in notepad you will get garbage and not human readable text. Explain in greater detail the problems you are having, is that when you try and read in the object using an ObjectInputStream it trows an exception?
darren85
yeah i know, but had a tutor go over my code last day and she said its my saving code thats not working but she couldn't see why, for reading it back in the code i have is,
if(e.getSource() == grid3Btn)
{
FileInputStream inputBookList;
ObjectInputStream ObjGetBookList;
try
{
num1Fld.setEnabled(false);
num2Fld.setEnabled(false);
num4Fld.setEnabled(false);
num5Fld.setEnabled(false);
grid2Btn.setEnabled(false);
//Declare stream objects
inputBookList = new FileInputStream("BookStore.txt");
ObjGetBookList = new ObjectInputStream(inputBookList);
System.out.println("a");
while (inputBookList != null)
{
System.out.print("b");
for(int i=0; i<5; i++)
{
BookList BkCurrent;
BkCurrent = (BookList) ObjGetBookList.readObject();
//ObjGetBookList.readObject(BkCurrent);
num1Fld.setText(BkCurrent.getBookName());
System.out.println(BkCurrent.getBookName());
//close file and object input streams
inputBookList.close();
ObjGetBookList.close();
}
}
}
OfflerCrocGod
darren85
wrote:
inputBookList.close();
ObjGetBookList.close();
}
}
}
Move those two lines outside the for loop, once you close it you wont be able to open it again other then that - which shouldn't stop you from reading at least one value - I can't see a huge problem, it should be OK. Care to tell me why your tutor came to the conclusion that it was your saving code? By the way you are trying something but you are not catching anything.....