import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;


public class ReadSequentialFile {

    private ObjectInputStream input;

    public void openFile()  {
        try {
            input = new ObjectInputStream(
                    new FileInputStream("clients.ser"));

        } catch (IOException ioException) {
            System.err.println("Error opening File");
        }
    }

    public void readRecords()  {
        AccountRecordSerializable record;
        System.out.printf("%-10s%-12s%-12s%10s%\n","Account", "FirstName", "LastName", "Balance");
        try {
            while (true) {
                record = (AccountRecordSerializable) input.readObject();
                System.out.printf("%-10s%-12s%-12s%10.2f\n",
                        record.getAccount(), record.getFirstname(),
                        record.getLastname(), record.getBalance());

            }
        } catch (EOFException endOfFileException) {
            return;
        } catch (ClassNotFoundException classNotFoundException) {
            System.err.println("Unable to create object");
        } catch (IOException ioException) {
            System.err.println("Error during reading from File.");
        }
    }

    public void closeFile()  {
        try {
            if (input != null) {
                input.close();
            }
            System.exit(0);
        } catch (IOException ioException) {
            System.err.println("Error closing File.");
            System.exit(1);
        }

    }
}
//the main class of Read Sequential file
public class ReadSequentialFileTest {

    public static void main(String[] args) {

        ReadSequentialFile apps = new ReadSequentialFile();
        apps.openFile();
        apps.readRecords();
        apps.closeFile();
    }
}


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;


public class CreateSequentialFile {

    private ObjectOutputStream output;

    public void OpenFile()   {
        try {
            output = new ObjectOutputStream(new FileOutputStream("client.ser"));
        } catch (IOException ex) {
            ex.printStackTrace();


        }

    }

    public void addrecords() {

        AccountRecordSerializable record;
        int accountNumber = 0;
        String firstName;
        String lastName;
        double balance;
        Scanner input = new Scanner(System.in);
        System.out.printf("%s\n%s", "Enter account number (>0), firstname, lastname, balance", "?");
        while (input.hasNext()) {
            accountNumber = input.nextInt();
            firstName = input.next();
            lastName = input.next();
            balance = input.nextDouble();
            if (accountNumber > 0) {
                record = new AccountRecordSerializable(accountNumber, firstName, lastName, balance);
                try {
                    output.writeObject(record);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            } else {
                System.out.println("accountNumber>0");
            }

        }
        System.out.printf("%s\n%s", "Enter account number (>0), firstname, lastname, balance", "?");
    }

    public void closeFile() {
        if (output != null) {
            try {
                output.close();

            } catch (IOException ex) {
            }
        }
    }
}

// the main class of Create Sequential file
import java.io.IOException;



public class CreateSequentialFileTest {


    public static void main(String[] args)  {
        CreateSequentialFile application = new CreateSequentialFile ();
        application.OpenFile();
        application.addrecords();
        application.closeFile();

    }

}

Hello guys, Can you please help me fix this codes??
I cant find where is my mistakes.
Thank you guys..

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Attachments
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author STUDES
 */
public class CreateSequentialFile {

    private ObjectOutputStream output;

    public void OpenFile()   {
        try {
            output = new ObjectOutputStream(new FileOutputStream("client.ser"));
        } catch (IOException ex) {
            ex.printStackTrace();


        }

    }

    public void addrecords() {

        AccountRecordSerializable record;
        int accountNumber = 0;
        String firstName;
        String lastName;
        double balance;
        Scanner input = new Scanner(System.in);
        System.out.printf("%s\n%s", "Enter account number (>0), firstname, lastname, balance", "?");
        while (input.hasNext()) {
            accountNumber = input.nextInt();
            firstName = input.next();
            lastName = input.next();
            balance = input.nextDouble();
            if (accountNumber > 0) {
                record = new AccountRecordSerializable(accountNumber, firstName, lastName, balance);
                try {
                    output.writeObject(record);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            } else {
                System.out.println("accountNumber>0");
            }

        }
        System.out.printf("%s\n%s", "Enter account number (>0), firstname, lastname, balance", "?");
    }

    public void closeFile() {
        if (output != null) {
            try {
                output.close();

            } catch (IOException ex) {
            }
        }
    }
}
import java.io.IOException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author STUDES
 */
public class CreateSequentialFileTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)  {
        CreateSequentialFile application = new CreateSequentialFile ();
        application.OpenFile();
        application.addrecords();
        application.closeFile();
        // TODO code application logic here
    }

}
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author STUDES
 */
public class ReadSequentialFile {

    private ObjectInputStream input;

    public void openFile()  {
        try {
            input = new ObjectInputStream(
                    new FileInputStream("clients.ser"));

        } catch (IOException ioException) {
            System.err.println("Error opening File");
        }
    }

    public void readRecords()  {
        AccountRecordSerializable record;
        System.out.printf("%-10s%-12s%-12s%10s%\n","Account", "FirstName", "LastName", "Balance");
        try {
            while (true) {
                record = (AccountRecordSerializable) input.readObject();
                System.out.printf("%-10s%-12s%-12s%10.2f\n",
                        record.getAccount(), record.getFirstname(),
                        record.getLastname(), record.getBalance());

            }
        } catch (EOFException endOfFileException) {
            return;
        } catch (ClassNotFoundException classNotFoundException) {
            System.err.println("Unable to create object");
        } catch (IOException ioException) {
            System.err.println("Error during reading from File.");
        }
    }

    public void closeFile()  {
        try {
            if (input != null) {
                input.close();
            }
            System.exit(0);
        } catch (IOException ioException) {
            System.err.println("Error closing File.");
            System.exit(1);
        }

    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author STUDES
 */
public class ReadSequentialFileTest {

    public static void main(String[] args) {

        ReadSequentialFile apps = new ReadSequentialFile();
        apps.openFile();
        apps.readRecords();
        apps.closeFile();
    }
}

Please use code tags when posting your code.

Please copy full text of error message and paste it here. Here is a sample:

TestSorts.java:138: cannot find symbol
symbol : variable var
location: class TestSorts
var = 2;
^

here is the error...
Error opening File
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '
'
at java.util.Formatter.checkText(Formatter.java:2502)
at java.util.Formatter.parse(Formatter.java:2484)
at java.util.Formatter.format(Formatter.java:2413)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at ReadSequentialFile.readRecords(ReadSequentialFile.java:31)
at ReadSequentialFileTest.main(ReadSequentialFileTest.java:19)
Java Result: 1

I checked it already but I cant Understand what is the error..

On line 31 your code calls the printf() method.
Double check the the args passed to printf() on that line.
Or post that line here.

This article has been dead for over six months. Start a new discussion instead.