Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/03/2016, 08:57
p3cn0G
 
Fecha de Ingreso: febrero-2014
Mensajes: 60
Antigüedad: 11 años
Puntos: 0
Problema al leer de fichero

Buenas tardes, estoy haciendo un ejercicio en el que dado un fichero binario “FNum.bi" que contenga 25 enteros (que los paso con un bucle for), hacer un algoritmo que escriba en un fichero “FNumOrd.dat” los valores ordenados del fichero de partida.

Bien, el codigo que tengo para resolver esto es el siguiente.




Código Java:
Ver original
  1. public class Ficheros6_1 {
  2.  
  3.     /**
  4.      * @param args the command line arguments
  5.      */
  6.     public static void main(String[] args) {
  7.          try{
  8.              
  9.             List<Integer> list_num = new ArrayList();
  10.             File f = new File("src//datos//FNum.bi");
  11.            
  12.             FileOutputStream fos = new FileOutputStream(f);
  13.             DataOutputStream dos = new DataOutputStream(fos);
  14.            
  15.             Random rand = new Random();
  16.            
  17.             for(int i = 0; i < 25; i++){
  18.                 int num = rand.nextInt(25)+65;
  19.                 dos.write(num);
  20.             }
  21.            
  22.             dos.close();
  23.            
  24.             FileInputStream fis = new FileInputStream(f);
  25.             DataInputStream dis = new DataInputStream(fis);
  26.            
  27.            
  28.             int dat = 0;
  29.            
  30.             while(dis.read() != -1){
  31.                 dat = dis.read();
  32.                 list_num.add(dat);
  33.             }
  34.            
  35.             dis.close();
  36.            
  37.             Collections.sort(list_num);
  38.             Collections.reverse(list_num);
  39.                    
  40.            
  41.            
  42.             File f2 = new File("src//datos//FNumOrd.dat");
  43.            
  44.             FileOutputStream fos2 = new FileOutputStream(f2);
  45.             DataOutputStream dos2 = new DataOutputStream(fos2);
  46.            
  47.            
  48.             while(!list_num.isEmpty()){
  49.                 dat = list_num.get(0);
  50.                 dos2.write(dat);
  51.                 list_num.remove(0);
  52.             }
  53.            
  54.             dos2.close();
  55.            
  56.            
  57.         }catch (FileNotFoundException ex)
  58.         {  
  59.             System.out.println("Ruta erronea");
  60.         }
  61.         catch (IOException ex)
  62.         {
  63.             System.out.println("Error: "+ex.getMessage());
  64.         }
  65.     }



Bien, a la hora de escribir los 25 numeros enteros en el fichero .bin, se hace correctamente, pero cuando intento leer posteriormente del mismo fichero .bin, solo lee 13 datos, y no 25.
A que puede ser debido esto??
Si no ha quedado claro el motivo de mi duda, diganlo y lo aclarare mas.
Gracias y un saludo