// Accesos aleatorios
import java.io.*;
import java.util.StringTokenizer;
public class Aleatorio
{
RandomAccessFile Archivo;
PrintStream PS = System.out; // Referencia a la salida est'andar
InputStream IS = System.in; // Referencia a la entrada est'andar
int NREGS = 0; // Numero de registros
int LongReg = 9; // Son 2 cadenas de 4 caracteres y una coma = 9
public Aleatorio() throws IOException
{
File F = new File("TEXTO.TXT");
String OK = "si";
if (F.exists()) OK = LeeDato("Ya existe, continuar? <si / no> ");
if (OK.equalsIgnoreCase("si"))
{
Archivo = new RandomAccessFile("TEXTO.TXT", "rw") ;
if (F.exists()) {NREGS = (int)(Archivo.length() / LongReg);}
PS.println("NREGS= " + (NREGS+1));
Ciclo_de_datos();
}
else PS.println("Listo!!"); // Se decicio no continuar y se termina el programa
}
String LeeDato(String Mensaje)
{
String Salida = "";
BufferedReader BR = new BufferedReader(new InputStreamReader(IS));
PS.print(Mensaje + " "); PS.flush();
try { Salida = BR.readLine(); } catch (IOException e){}
return Salida;
}
public static void main(String args[]) throws IOException { new Aleatorio(); }
// Si es mayor el indice a la cantidad de registros, se almacena al final
// Si si existe el registro Indice, se reemplaza su contenido
public void SAVE(StringTokenizer ST) throws IOException
{
int Indice = 0;
if (ST.countTokens() == 3) // Se recibe: Indice Dato Dato
{
Indice = Integer.parseInt(ST.nextToken()) - 1; // Menos uno porque empieza en cero
if (Indice > NREGS) { NREGS++; Archivo.seek(NREGS * LongReg); } // Si es mayor, se va al final
else Archivo.seek(Indice * LongReg); // Busca la posicion solicitada
Archivo.writeBytes(ST.nextToken() + "," + ST.nextToken());
}
else System.out.println("Numero de parametros incorrecto");
}
public void READ(StringTokenizer ST) throws IOException
{
int Indice = 0;
byte[] LECTURA = new byte[LongReg];
if (ST.countTokens() == 1) // Se recibe sOlo el Indice
{
Indice = Integer.parseInt(ST.nextToken())-1;
if (Indice > NREGS) PS.println("No existe el registro...");
else if (Indice <= NREGS && NREGS != 0) // mas uno por la referencia al usuario
{
Archivo.seek(Indice * LongReg);
Archivo.readFully(LECTURA, 0, LongReg);
SALIDA(new StringTokenizer(new String(LECTURA),","));
}
else PS.println("Conflicto al buscar el registro...");
}
else PS.println("Numero de parametros incorrecto");
}
void SALIDA(StringTokenizer ST)
{
PS.println("Palabra 1= " + ST.nextToken());
PS.println("Palabra 2= " + ST.nextToken());
}
//
void Ciclo_de_datos() throws IOException
{
String Peticion = "", Instr;
StringTokenizer ST;
while (!Peticion.equalsIgnoreCase("x"))
{
PS.println("_______________________________");
PS.println("L=Leer, G=Guardar, #=Indice");
PS.println("Sintaxis: [G # Dato] o [L #]");
PS.println("_______________________________");
Peticion = LeeDato("Instruccion: <x=salir>");
ST = new StringTokenizer(Peticion);
Instr = ST.nextToken();
if (Instr.equalsIgnoreCase("L")) READ(ST);
if (Instr.equalsIgnoreCase("G")) SAVE(ST);
}
Archivo.close();
PS.println("By ...");
}
}