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


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

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

  1. #1
    Join Date
    May 2006
    Posts
    3

    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...

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Have you thought about using the nio classes, especially channels?

  3. #3
    Join Date
    May 2006
    Posts
    3
    dont know what you mean, im a beginner (3 hrs a week for 10 weeks) we havent come across that yet!

    Ant...

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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.

  5. #5
    Join Date
    Mar 2004
    Posts
    635
    But for future reference, please post your code inside code tags so it stays indented. Makes things a lot easier to read.

  6. #6
    Join Date
    May 2006
    Posts
    3
    ok then, will do.
    Thanks for the compliment!
    I start intermediate JAVA next september!
    Ant..

  7. #7
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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

  1. How to read objects from file in a loop
    By mail2vinaybabu in forum Java
    Replies: 10
    Last Post: 02-27-2005, 02:07 PM
  2. Replies: 3
    Last Post: 02-23-2003, 03:05 AM
  3. Replies: 1
    Last Post: 04-02-2002, 11:45 PM
  4. Replies: 0
    Last Post: 11-19-2001, 01:11 AM
  5. read file poblems after successful login
    By elissee in forum Java
    Replies: 0
    Last Post: 06-01-2000, 04: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center