-
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...
-
Have you thought about using the nio classes, especially channels?
-
dont know what you mean, im a beginner (3 hrs a week for 10 weeks) we havent come across that yet!
Ant...
-
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.
-
But for future reference, please post your code inside code tags so it stays indented. Makes things a lot easier to read.
-
ok then, will do.
Thanks for the compliment!
I start intermediate JAVA next september!
Ant..
-
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.
Similar Threads
-
By mail2vinaybabu in forum Java
Replies: 10
Last Post: 02-27-2005, 01:07 PM
-
Replies: 3
Last Post: 02-23-2003, 02:05 AM
-
By Larry Rebich in forum vb.announcements
Replies: 1
Last Post: 04-02-2002, 10:45 PM
-
By Kyle Gabhart in forum Java
Replies: 0
Last Post: 11-19-2001, 12:11 AM
-
Replies: 0
Last Post: 06-01-2000, 03:14 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks