Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Cannot append file (or read now), why wont it work?

Hello, i could read and write, but when i re-ran the program i could only read. Ive added code so i can remove the header so i can write after re-launching. Now i can read or write!

Does anyone know whats wrong?
------------------
The Main Class -->
------------------

import java.io.*;
import javax.swing.*;

public class form extends javax.swing.JFrame {

ObjectOutputStream Output;
ObjectInputStream Input;
File file = new File(System.getProperty("user.dir") + "/bookdata/book.txt");

/** Creates new form form */
public form() {
initComponents();
}

private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
txtIsbn.setText("");
txtTitle.setText("");
txtAuthor.setText("");
txtPrice.setText("");
txtSupplierno.setText("");
txtLastorderdate.setText("");
}

private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {
bookRecord record;
try{
record = (bookRecord)Input.readObject();
txtIsbn.setText(record.getIsbn());
txtTitle.setText(record.getTitle());
txtAuthor.setText(record.getAuthor());
txtPrice.setText(Float.toString(record.getPrice()));
txtSupplierno.setText(Integer.toString(record.getSupplierno()));
txtLastorderdate.setText(Integer.toString(record.getLastorderdate()));
} catch(IOException c){
JOptionPane.showMessageDialog(this, "No More Records To Show!", "Error", JOptionPane.ERROR_MESSAGE);
} catch(ClassNotFoundException d){
JOptionPane.showMessageDialog(this, "Class Not Found Error!", "Error", JOptionPane.ERROR_MESSAGE);
} catch(NullPointerException e){
JOptionPane.showMessageDialog(this, "Null Pointer!", "Error", JOptionPane.ERROR_MESSAGE);
}
}

private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {
bookRecord record;
System.out.println(file);
try{
Input = new ObjectInputStream(new FileInputStream(file));
record = (bookRecord)Input.readObject();
txtIsbn.setText(record.getIsbn());
txtTitle.setText(record.getTitle());
txtAuthor.setText(record.getAuthor());
txtPrice.setText(Float.toString(record.getPrice()));
txtSupplierno.setText(Integer.toString(record.getSupplierno()));
txtLastorderdate.setText(Integer.toString(record.getLastorderdate()));
} catch(IOException c){
JOptionPane.showMessageDialog(this, "No first record exists!", "Error", JOptionPane.ERROR_MESSAGE);
} catch(ClassNotFoundException d){
JOptionPane.showMessageDialog(this, "Class Not Found Error!", "Error", JOptionPane.ERROR_MESSAGE);
}
}

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
bookRecord record;
System.out.println(file);


try{
record = new bookRecord(
txtIsbn.getText(),
txtTitle.getText(),
txtAuthor.getText(),
Float.parseFloat(txtPrice.getText()),
Integer.parseInt(txtSupplierno.getText()),
Integer.parseInt(txtLastorderdate.getText()));
Output.writeObject(record);

System.out.println(record);

} catch(IOException b){
JOptionPane.showMessageDialog(this, "Input/Output Error!", "Error", JOptionPane.ERROR_MESSAGE);
} catch(NumberFormatException d){
JOptionPane.showMessageDialog(this, "Textbox Format Error!", "Error", JOptionPane.ERROR_MESSAGE);
} catch(NullPointerException e){
JOptionPane.showMessageDialog(this, "Null Pointer!", "Error", JOptionPane.ERROR_MESSAGE);
}
}

private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {

try{
Output = new HeadlessObjectOutputStream(new FileOutputStream(file,true));
} catch(IOException a){
JOptionPane.showMessageDialog(this, "Input/Output Error!", "Error", JOptionPane.ERROR_MESSAGE);
}
}

------------------
The bookRecord Class-->
------------------

import java.io.*;
import javax.swing.*;

public class bookRecord implements Serializable{

private String Isbn;
private String Title;
private String Author;
private float Price;
private int Supplierno;
private int Lastorderdate;

public bookRecord() {
this("","","",0,0,0);
}

public bookRecord(String i, String t, String a, float p, int s, int l) {
setIsbn(i);
setTitle(t);
setAuthor(a);
setPrice(p);
setSupplierno(s);
setLastorderdate(l);
}

public void setIsbn(String i){
Isbn = i;
}

public String getIsbn(){
return Isbn;
}

public void setTitle(String t){
Title = t;
}

public String getTitle(){
return Title;
}

public void setAuthor(String a){
Author = a;
}

public String getAuthor(){
return Author;
}

public void setPrice(float p){
Price = p;
}

public float getPrice(){
return Price;
}

public void setSupplierno(int s){
Supplierno = s;
}

public int getSupplierno(){
return Supplierno;
}

public void setLastorderdate(int l){
Lastorderdate = l;
}

public int getLastorderdate(){
return Lastorderdate;
}
}

class HeadlessObjectOutputStream extends ObjectOutputStream{
public HeadlessObjectOutputStream(OutputStream Output) throws IOException{
super(Output);
}
protected void writeStreamHeader() throws IOException{
}
}

Thanks, Very much, Antony...
[7062 byte] By [sirantonycartwr] at [2007-11-11 8:43:36]
# 1 Re: Cannot append file (or read now), why wont it work?
Have you thought about using the nio classes, especially channels?
nspils at 2007-11-11 22:34:32 >
# 2 Re: Cannot append file (or read now), why wont it work?
dont know what you mean, im a beginner (3 hrs a week for 10 weeks) we havent come across that yet!

Ant...
sirantonycartwr at 2007-11-11 22:35:32 >
# 3 Re: Cannot append file (or read now), why wont it work?
Your code is pretty advanced for a beginner. It looked to me that you were at a level of sophistication that you would understand what channels are. I think it would be helpful for you to research and learn this.
nspils at 2007-11-11 22:36:31 >
# 4 Re: Cannot append file (or read now), why wont it work?
But for future reference, please post your code inside code tags so it stays indented. Makes things a lot easier to read.
Phaelax at 2007-11-11 22:37:31 >
# 5 Re: Cannot append file (or read now), why wont it work?
ok then, will do.
Thanks for the compliment!
I start intermediate JAVA next september!
Ant..
sirantonycartwr at 2007-11-11 22:38:40 >
# 6 Re: Cannot append file (or read now), why wont it work?
Great! Keep up the study!

I could not find where you "close" the ObjectOutputStream (which will release the File) when you finish writing to it. The File, and the stream, will be in a state which is "locked", because they are waiting for more input from you. They remain in "read only" mode until they are unlocked. Once you "close" the stream, you can read and write from the file.
nspils at 2007-11-11 22:39:34 >