Advertisement
xlujiax

LeerArchivios

Jul 4th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. --LeerArchivios
  2. package archivos;
  3.  
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6.  
  7. public class LeerArchivo {
  8.    
  9.    
  10.     public void leer_desde_archivo_Bytes(String nombre_archivo) {
  11.         try {
  12.             FileInputStream r = new FileInputStream(nombre_archivo);
  13.            
  14.             //Obtiene la cantidad de bytes del archivo
  15.             int a = r.available();
  16.             int nByte = 0;
  17.            
  18.             int d = r.read();
  19.            
  20.             while ( d != -1) {
  21.                 ++nByte;
  22.                
  23.                 System.out.print( (char) d);
  24.                
  25.                 d = r.read();
  26.             }
  27.             r.close();
  28.             System.out.println("\n Cantidad de Bytes leidos =" + nByte);
  29.             System.out.println("Cantidad de Bytes en el archivo =" + a);
  30.            
  31.         } catch (IOException e) {
  32.             System.out.println("\n### IO Error: " + e);
  33.         }
  34.     }
  35.  
  36.     public static void main(String[] args) {
  37.         // TODO Auto-generated method stub
  38.  
  39.     }
  40.  
  41. }
  42. --EscribirArchivos
  43. package archivos;
  44.  
  45. import java.io.FileOutputStream;
  46. import java.io.IOException;
  47.  
  48. public class EscribirArchivo {
  49.  
  50.     public void escribir_archivo_Byte(String nombre_archivo) {
  51.         try {
  52.             FileOutputStream r = new FileOutputStream(nombre_archivo);
  53.            
  54.             int nByte = 0;
  55.             int d = System.in.read();
  56.            
  57.             while(d != 4) {
  58.                 ++nByte;
  59.                 r.write(d);
  60.                 d = System.in.read();
  61.             }
  62.             r.close();
  63.         } catch (IOException e) {
  64.             System.out.println("\n### IO Error: " + e);
  65.         }
  66.     }
  67. }
  68.  
  69. --Main
  70. package archivos;
  71.  
  72. public class TesterLeerArchivo {
  73.  
  74.     public static void main(String[] args) {
  75.        
  76.         LeerArchivo la = new LeerArchivo();
  77.         la.leer_desde_archivo_Bytes("D:\\Usil\\POO\\Semana 10\\Parte A\\prueba.txt");
  78.  
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement