Foros del Web » Programación para mayores de 30 ;) » Java »

Archivo aleatorio, clase para leer y clase para escribir

Estas en el tema de Archivo aleatorio, clase para leer y clase para escribir en el foro de Java en Foros del Web. Hola buenas, tengo que hacer 2 clases, una para leer unos datos y meterlos en un .dat y otro que mostrara esta informacion, no se ...
  #1 (permalink)  
Antiguo 01/12/2014, 09:52
Avatar de muchuelu93  
Fecha de Ingreso: noviembre-2013
Mensajes: 35
Antigüedad: 10 años, 5 meses
Puntos: 2
Exclamación Archivo aleatorio, clase para leer y clase para escribir

Hola buenas, tengo que hacer 2 clases, una para leer unos datos y meterlos en un .dat y otro que mostrara esta informacion, no se por que no se me posiciona bien al mostrar los datos, creo que he contado bien los byts y todo pero no me lo muestra bien... alguien ve el error?

CLASE PARA ESCRIBIR
Código HTML:
import java.io.*;
public class Ex3 
{
	//Escriure
	public static void main(String[] args) throws IOException
	{
		File fil = new File("empleat.dat");
		RandomAccessFile file = new RandomAccessFile(fil, "rw");
		
		//Arrays de cognoms, salari
		String apellido[] = {"Reimundo", "Umelos", "Josum"};
		int departament[] = {1, 2, 3};
		Double sou[] = {1000.45, 3200.98, 9882.72};
		
		StringBuffer buffer = null;
		
		for(int i=0; i<apellido.length; i++)
		{
			file.write(i+1);//usem i+1 per identificar el usuari
			buffer = new StringBuffer(apellido[i]);
			buffer.setLength(10);
			
			file.writeChars(buffer.toString());
			file.writeInt(departament[i]);
			file.writeDouble(sou[i]);
		}
		file.close();
	}
}


CLASE PARA LEER
Código HTML:
import java.io.*;
import java.util.*;
public class Ex3_1 
{
	//Llegir
	public static void main(String[] args) throws IOException
	{
		File arxiu = new File("empleat.dat");
		RandomAccessFile file = new RandomAccessFile(arxiu, "rw");
		
		//variables
		int id, departament, posicion;
		Double sou;
		char apellido[] = new char[10], aux;
		
		//Creem l'escaner
		Scanner scan = new Scanner(System.in);
		
		//Demenem la ID
		System.out.println("Instrodueix la ID d'un empleat: ");
		int identificador;
		identificador = scan.nextInt();
		
		//Demenem la cantitat a sumar
		System.out.println("Instrodueix l'import a sumar: ");
		Double importe;
		importe = scan.nextDouble();
		
		posicion = (identificador - 1)*36;
		
		if(posicion >= file.length())
		{
			System.out.println("Aquest empleat no esta");
		}
		else
		{
			file.seek(posicion);
			id = file.readInt();
			for(int i=0; i<apellido.length; i++)
			{
				aux=file.readChar();
				apellido[i]=aux;
				System.out.print(apellido[i]);
			}
			String apellidoS = new String(apellido);
			departament = file.readInt();
			sou = file.readDouble();
			
			importe = sou + importe;
			
			posicion = posicion + 4 + 20 + 4;
			
			file.seek(posicion);
			file.writeDouble(importe);
			
			System.out.println("Empleat: "+apellidoS);
			System.out.println("Salari anterior: "+sou);
			System.out.println("Salari actualizat: "+importe);
		}
		scan.close();
		file.close();
	}
}

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:20.