IOException

1 resposta
R

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 d then press Enter
On windows type z then press Enter

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)

1 Resposta

ErickRAR

chame o printStrackTrace nos seus catchs. A mensagem de erro completa serve para ajudar a saber o quê está errado. Se você não ver a mensagem em lugar algum e trocar sempre por System.out.println’s mostrando só “Erro aqui.”, “Erro ali” , nunca saberá direito o que está acontecendo.

Criado 7 de setembro de 2014
Ultima resposta 8 de set. de 2014
Respostas 1
Participantes 2