Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Tanda2;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- public class Ejercicio1 {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- /*
- * crear fichero binario de ints nums.bin (los que se deseen)
- * de ese fichero,generar uno de texto nums.txt con una linea por cada int
- * de nums1.bin
- * Saber cuanto ocupara un entero
- *
- * Integer.size/8;
- *
- * para conoer el tamaño de ficheroen bytes
- *
- * 1º Crear una instancia de file y luego utilizar el .length
- * File f = new File ("...");
- * bytes=f.length
- *
- *
- * 2ºEn los DATAinput y FILEINPUT
- * bytes=f1.available(); mostrara la cantidad de bytes disponibles
- *
- * bytes/Integer.size/8;
- *
- * DataInput read write
- */
- // BufferedReader fb=new BufferedReader (new FileReader("nums.bin"));
- // BufferedWriter ft=new BufferedWriter (new FileWriter("nums.txt"));
- char resp='S';
- int n;
- DataOutputStream fs=new DataOutputStream(new FileOutputStream("nums.bin"));
- System.out.println("Introduzca numero ");
- n=Consola.leeInt();
- fs.writeInt(n);
- System.out.println("Quieres otro?");
- resp=Consola.leeChar();
- while(resp=='S' || resp == 's'){
- System.out.println("Introduzca numero ");
- n=Consola.leeInt();
- fs.writeInt(n);
- System.out.println("Quieres otro?");
- resp=Consola.leeChar();
- }
- fs.close();
- //generamos un bucle para que cada vez que lea un numero lo escriba
- DataInputStream fb = new DataInputStream( new FileInputStream("nums.bin"));
- PrintWriter ft =new PrintWriter (new FileWriter ("nums.txt"));
- int bytes = fb.available();
- for ( int i = 1 ; i <= bytes / (Integer.SIZE / 8) ; i++ )
- {
- ft.println( fb.readInt() );
- }
- fb.close();
- ft.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment