Galera,não consigo descobrir aonde está o erro nesse código (inclusive está no livro Deitel 6a edição),acontece o seguinte: eu executo o programa normalmente,entro com os 4 valores e ai ele me retorna uma IOException ("Error writing to file.").
package Arquivos_e_Fluxos;
import com.deitel.jhtp6.ch14.AccountRecordSerializable;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class CreateSequentialFile
{
private ObjectOutputStream output;
public void openFile()
{
try
{
output = new ObjectOutputStream(new FileOutputStream("clients.ser"));
}catch(IOException ioexception)
{
System.err.println("Error opening file.");
}
}
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\n%s\n%s\n\n",
"To terminate input, type the end-of-line indicador",
"when you are prompted to enter input.",
"On UNIX/Linux/Mac OS X type <crtl> d then press Enter",
"On windows type <crtl> z then press Enter");
System.out.printf("%s\n%s","Enter account number (>0),first name,last name and balance.","?");
while(input.hasNext())
{
try
{
accountNumber = input.nextInt();
firstName = input.next();
lastName = input.next();
balance = input.nextDouble();
if(accountNumber > 0)
{
record = new AccountRecordSerializable(accountNumber,firstName,lastName,balance);
output.writeObject(record);
}else
{
System.out.println("Account number must be greater than 0.");
}
}catch(IOException ioException)
{
System.err.println("Error writing to file.");
return;
}catch(NoSuchElementException elementException)
{
System.err.println("Invalid input.Please try again");
input.nextLine();
}
System.out.printf("%s %s\n%s","Enter account number (>0),","first name,last name and balance.","?");
}
}
public void closeFile()
{
try
{
if(output != null)
{
output.close();
}
}catch(IOException ioException)
{
System.err.println("Error closing file.");
System.exit(1);
}
}
}
package Arquivos_e_Fluxos;
public class CreateSequentialFileTest
{
public static void main(String args[])
{
CreateSequentialFile application = new CreateSequentialFile();
application.openFile();
application.addRecords();
application.closeFile();
}
}
ERRO :
run:
To terminate input, type the end-of-line indicador
when you are prompted to enter input.
On UNIX/Linux/Mac OS X type
On windows type
Enter account number (>0),first name,last name and balance.
?10 renan renan 20
Error writing to file.
CONSTRUÍDO COM SUCESSO (tempo total: 5 segundos)