Check the new version here

Popular channels

Java - Archivos randomicos

He aqui el jeroglífico


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import javax.swing.JOptionPane;
public class Main {
static File f= new File("O:/misdatos/archi21.txt"; );
static RandomAccessFile raf ;
public static void main(String[] args) throws IOException {
alta();
listado();
/// busca();
}

static void busca () throws IOException{

int cb = Integer.parseInt(JOptionPane.showInputDialog("Ingrese costo a buscar " )) ;
int posReg = ubicaCosto(cb);
if(posReg == -1){
JOptionPane.showMessageDialog(null , "Costo inexistente"; ) ;
}
else
{
raf = new RandomAccessFile(f, "r"; ) ;
raf.seek(posReg);
boolean flag = raf.readBoolean() ;
int costo = raf.readInt();
double sueldo = raf.readDouble() ;
String pais = raf.readUTF();
String reg = " estado = " + flag + " n costo = "+ costo + "n sueldo = "+ sueldo + "n pais = " + pais;
JOptionPane.showMessageDialog(null, reg) ;
}
}
private static int ubicaCosto(int cb) throws FileNotFoundException {
raf = new RandomAccessFile(f, "r" ) ;
System.out.println("Listado del archivo" ) ;
int p = 0 , ub = -1;
try {

while(true){
raf.seek(p);
boolean flag = raf.readBoolean() ;
int costo = raf.readInt();
double sueldo = raf.readDouble() ;
String pais = raf.readUTF();
if(costo == cb){
ub = p;
}
p = p+25;
}
}catch(Exception e ){

}
return ub;

}

static void listado() throws FileNotFoundException {
raf = new RandomAccessFile(f, "r"; ) ;
System.out.println("Listado del archivo"; ) ;
int p = 0;
try{
while(true){
raf.seek(p ) ;
boolean flag = raf.readBoolean() ;
int costo = raf.readInt();
double sueldo = raf.readDouble() ;
String pais = raf.readUTF() ;
System.out.println(flag + " "+ costo + " "+ sueldo + " " + pais) ;
p = p +25;
}
}
catch(Exception e)
{

}

}
static void alta () throws IOException{
raf = new RandomAccessFile(f, "rw"; ) ;
int p = (int) raf.length( ) ;
System.out.println(" Len = " + p);
raf.seek(p);
raf.writeBoolean(true);
int costo = Integer.parseInt(JOptionPane.showInputDialog("Ingrese costo" ) ) ;
raf.writeInt(costo) ;
double sueldo = Double.parseDouble(JOptionPane.showInputDialog("Ingrese sueldo" ) ) ;
raf.writeDouble(sueldo);
String pais = JOptionPane.showInputDialog("Ingrese el pais" ) ;
String npais = control(pais, 10);
raf.writeUTF(npais);
raf.close();
}

static String control (String nom, int k){
if(nom.length()>10)
{
nom = nom.substring(1, k);
}
else
{
while(nom.length(){
nom = nom + " ";
}
}
return nom;
}
}
0
0
0
0No comments yet