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