Txerrinko

Tema 7 ejercicio 1 tanda2

Mar 27th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. package Tanda2;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.DataInputStream;
  6. import java.io.DataOutputStream;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.FileReader;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.io.PrintWriter;
  14.  
  15.  
  16.  
  17. public class Ejercicio1 {
  18.  
  19.     /**
  20.      * @param args
  21.      * @throws IOException
  22.      */
  23.     public static void main(String[] args) throws IOException {
  24.         // TODO Auto-generated method stub
  25.      
  26.         /*
  27.          * crear fichero binario de ints nums.bin (los que se deseen)
  28.          * de ese fichero,generar uno de texto nums.txt con una linea por cada int
  29.          * de nums1.bin
  30.          * Saber cuanto ocupara un entero
  31.          *
  32.          * Integer.size/8;
  33.          *
  34.          * para conoer el tamaño de ficheroen bytes
  35.          *
  36.          * 1º Crear una instancia de file y luego utilizar el .length
  37.          * File f = new File ("...");
  38.          * bytes=f.length
  39.          *
  40.          *
  41.          * 2ºEn los DATAinput  y FILEINPUT
  42.          * bytes=f1.available(); mostrara la cantidad de bytes disponibles
  43.          *
  44.          * bytes/Integer.size/8;
  45.          *
  46.          * DataInput   read    write
  47.          */
  48.        
  49.    
  50.        
  51.     //  BufferedReader fb=new BufferedReader (new FileReader("nums.bin"));
  52.     //  BufferedWriter ft=new BufferedWriter (new FileWriter("nums.txt"));
  53.        
  54.         char resp='S';
  55.         int n;
  56.         DataOutputStream fs=new DataOutputStream(new FileOutputStream("nums.bin"));
  57.        
  58.        
  59.        
  60.             System.out.println("Introduzca numero ");
  61.              n=Consola.leeInt();
  62.              fs.writeInt(n);
  63.            
  64.              System.out.println("Quieres otro?");
  65.              resp=Consola.leeChar();
  66.        
  67.             while(resp=='S' || resp == 's'){
  68.                 System.out.println("Introduzca numero ");
  69.                 n=Consola.leeInt();
  70.                
  71.             fs.writeInt(n);        
  72.             System.out.println("Quieres otro?");
  73.              resp=Consola.leeChar();
  74.        
  75.              
  76.         }
  77.         fs.close();
  78.        
  79.             //generamos un bucle para que cada vez que lea un numero lo escriba
  80.        
  81.  
  82.        
  83.         DataInputStream fb  = new DataInputStream( new FileInputStream("nums.bin"));
  84.         PrintWriter ft =new PrintWriter (new FileWriter ("nums.txt"));
  85.         int bytes = fb.available();
  86.        
  87.         for ( int i = 1 ; i <= bytes / (Integer.SIZE / 8) ; i++ )
  88.         {
  89.             ft.println( fb.readInt() );
  90.         }
  91.        
  92.         fb.close();
  93.         ft.close();
  94.         }
  95.      
  96.        
  97.    
  98.        
  99.        
  100.        
  101.    
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment