Escrever dados em arquivo

2 respostas
PePeLeGaL

pessoal,

tenho o programa abaixo e esta acontecendo uma excecao na hora de escrever dados no arquivo. Alguem sabe como resolvo esse problema ?

TransactionProcessor.java

private FileEditor dataFile;

System.out.printf("\n%s%s\n%s\n%s", 
							"Enter account number,",
							"first name, amount and price.",
							"(Account number must be 1-100)", "? ");
					
					accountNumber = input.nextInt(); //le o numero de conta
					firstName = input.next();	//le o primeiro nome
					quantidade = input.nextInt();	//le o sobrenome
					balance = input.nextDouble(); //le o saldo
					
					dataFile.newRecord(accountNumber, firstName, quantidade, balance);
import java.io.IOException;
import java.io.RandomAccessFile;


public class RandomAccessAccountRecord extends AccountRecord
{
	public static final int SIZE = 46;
	
	//construtor sem argumentos chama outro construtor com valores-padrao
	public RandomAccessAccountRecord()
	{
		this(0, "", 0, 0.0);
	}
	
	//inicializa uma RandomAccessAccountRecord
	public RandomAccessAccountRecord(int account, String firstName, int quantidade, double balance)
	{
		super(account, firstName, quantidade, balance);
	}
	
	//le um registro em um RandomAccessFile especificado
	public void read(RandomAccessFile file) throws IOException
	{
		setAccount( file.readInt() );
		setFirstName( readName(file) );
		setQuantidade( file.readInt() );
		setBalance( file.readDouble() );
	}
	
	//assegura que o nome tenha um comprimento adequado
	private String readName( RandomAccessFile file ) throws IOException
	{
		char name[] = new char[15], temp;
		
		for(int count = 0; count < name.length; count++)
		{
			temp = file.readChar();
			name[count] = temp;
		}
		
		return new String(name).replace('\0',' ');
	}
	
	//grava um registro no RandomAccessFile especificado
	public void write( RandomAccessFile file ) throws IOException
	{
		file.writeInt( getAccount() );
		writeName( file, getFirstName() );
		file.writeInt( getQuantidade() );
		file.writeDouble( getBalance() );
	}
	
	//grava um nome no arquivo; maximo de 15 caracteres
	private void writeName(RandomAccessFile file, String name) throws IOException
	{
		StringBuffer buffer = null;
		
		if(name != null)
			buffer = new StringBuffer(name);
		else
			buffer = new StringBuffer(15);
		
		buffer.setLength(15);
		file.writeChars( buffer.toString() );
	}
}
FileEditor.java

//obtem um registro do arquivo
	public RandomAccessAccountRecord getRecord(int accountNumber)
		throws IllegalArgumentException, NumberFormatException, IOException
	{
		RandomAccessAccountRecord record = new RandomAccessAccountRecord();
		
		if(accountNumber < 1 || accountNumber > 100)
			throw new IllegalArgumentException("Out of range");
		
		//busca o registro apropriado no arquivo
		file.seek((accountNumber-1)*RandomAccessAccountRecord.SIZE);
		
		record.read(file);
		
		return record;
	}

//adiciona o registro ao arquivo
	public void newRecord(int accountNumber, String firstName, int quantidade, double balance)
		throws IllegalArgumentException, IOException
	{
		RandomAccessAccountRecord record = getRecord(accountNumber);
		
		if(record.getAccount() != 0)
		{
			throw new IllegalArgumentException("Account already exists");
		}
		
		//busca o registro apropriado no arquivo
		file.seek( (accountNumber-1) * RandomAccessAccountRecord.SIZE );
		
		record = new RandomAccessAccountRecord(accountNumber, firstName, quantidade, balance);
		
		record.write(file);
	}

2 Respostas

pintofree

Escreve a excessao q aparece ai pra ficar mais facil achar o problema!

PePeLeGaL
Error writing to the file.

eh uma IOException

Criado 18 de fevereiro de 2008
Ultima resposta 18 de fev. de 2008
Respostas 2
Participantes 2