Ver Mensaje Individual
  #1 (permalink)  
Antiguo 26/02/2014, 10:28
daniviso92
 
Fecha de Ingreso: abril-2013
Mensajes: 2
Antigüedad: 11 años
Puntos: 0
Pregunta Ordenar fichero binario

Hola buenas, estoy liado con un programita en java que debe coger dos ficheros binarios solo con enteros que se mezclen esos ficheros en otro y se ordenen de menor a mayor y se eliminen los repetidos... si alguien me ayuda le estaré muy agradecido ya que llevo tiempo con esto.

Código Java:
Ver original
  1. public class ProcesaFicheros {
  2.  
  3.     public static File mezclarFichero(File F1, File F2) {
  4.         File fsalida = null; // Fichero resultante
  5.  
  6.         try {
  7.             if (!F1.exists() || !F2.exists())
  8.                 System.out.println("Ficheros no encontrados");
  9.             else {
  10.                
  11.        
  12.                 InputStream f1= new FileInputStream(F1);
  13.                 InputStream f2= new FileInputStream(F2);
  14.                
  15.                 fsalida = new File("res" + File.separator + "Resultado.dat");
  16.                
  17.                 OutputStream f3= new FileOutputStream(fsalida);
  18.                
  19.                
  20.                
  21.                 byte[] buffer= new byte[256];
  22.  
  23.                
  24.                 while (true) {
  25.                   int n= f1.read(buffer);
  26.                   if (n < 0)
  27.                     break;
  28.                   f3.write(buffer, 0, n);
  29.                 }
  30.                
  31.                 while (true) {
  32.                   int n= f2.read(buffer);
  33.                   if (n < 0)
  34.                     break;
  35.                   f3.write(buffer, 0, n);
  36.                 }
  37.  
  38.                 // Cierre de flujos
  39.                 f1.close();
  40.                 f2.close();
  41.  
  42.                 f3.close();
  43.             }
  44.         } catch (IOException e) {
  45.             System.out.println("Error al acceder al fichero Mezcla");
  46.         } catch (Exception e) {
  47.             System.out.println(e.getMessage());
  48.         }
  49.  
  50.         return fsalida;
  51.     }
  52.  
  53.     public static void mostrarFichero(File f) {
  54.         try {
  55.  
  56.             if (!f.exists())
  57.                 System.out.println("Fichero no encontrado: "+ f.getName() );
  58.             else {
  59.                 // Asociamos el flujo de acceso directo a los datos binarios del
  60.                 // descriptor (MODO LECTURA)
  61.                 RandomAccessFile fichero = new RandomAccessFile(f, "r");
  62.  
  63.                 while (fichero.getFilePointer() < fichero.length()) {
  64.                     System.out.print("Posición: " + fichero.getFilePointer());
  65.                     System.out.print(" Valor: " + fichero.readInt() + "\n");
  66.  
  67.                     // Actualizo posición del puntero
  68.                     // Uso del puntero para posicionarnos
  69.                     fichero.seek(fichero.getFilePointer());
  70.                 }
  71.  
  72.  
  73.                 fichero.close();
  74.             }
  75.         } catch (IOException e) {
  76.             System.out.println("Error al acceder al fichero");
  77.         } catch (Exception e) {
  78.             System.out.println(e.getMessage());
  79.         }
  80.     }
  81.  
  82.     public static void main(String[] args) {
  83.         File f1 = new File("res" + File.separator + "Desordenado.dat");
  84.         File f2 = new File("res" + File.separator + "Aleatorio.dat");
  85.         File f3 = new File("res" + File.separator + "Resultado.dat");
  86.         File f4 = new File("res" + File.separator + "DatosRepetidosBorrados.dat");
  87.  
  88.         System.out.println("->  FICHERO 1 " + f1.getAbsolutePath() + ":");
  89.         ProcesaFicheros.mostrarFichero(f1);
  90.  
  91.         System.out.println("\n->  FICHERO 2 " + f2.getAbsolutePath() + ":");
  92.         System.out.println("=========");
  93.         ProcesaFicheros.mostrarFichero(f2);
  94.  
  95.         /* Fichero de mezcla: 1) File mezclarFichero (File F1, File F2) */
  96.         File fsalida = ProcesaFicheros.mezclarFichero(f1, f2);
  97.  
  98.         if (fsalida != null) {
  99.             System.out.println("\n->  FICHERO MEZCLA RESULTANTE "+ f3.getAbsolutePath() + ":");
  100.             System.out.println("=========");
  101.             ProcesaFicheros.mostrarFichero(f3);
  102.         }
  103.        
  104.         System.out.println("\n->  FICHERO Datos repetidos borrados y ordenados " + f4.getAbsolutePath() + ":");
  105.         System.out.println("=========");
  106.         ProcesaFicheros.mostrarFichero(f4);
  107.     }
  108. }