Advertisement
LEANDRONIEVA

escribirtxt

Sep 13th, 2023
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4.  
  5. public class EscribirArchivo {
  6.     File archivo;
  7.     private void crearArchivoDeTexto() {
  8.    
  9.         archivo = new File("archivo.txt");
  10.     }
  11.    
  12.     private void escribirArchivo(String numeros) {
  13.         try {
  14.             FileWriter escritura = new FileWriter(archivo);
  15.             escritura.write(numeros, 0, numeros.length());
  16.             escritura.close();
  17.            
  18.             System.out.println("Archivo escrito con éxito");
  19.         }catch(IOException e) {
  20.             e.printStackTrace(System.out);
  21.         }
  22.     }
  23.    
  24.     private static String generarNumeros() {
  25.         String numeros = "";
  26.         int aux;
  27.        
  28.         for(int i = 1; i<5000;i++) {
  29.             aux = Helper.random.nextInt(1,9999);
  30.             numeros += aux+" ";
  31.         }
  32.         return numeros;
  33.     }
  34.  
  35.     public static void main(String[] args) {
  36.         // TODO Auto-generated method stub
  37.  
  38.         EscribirArchivo archivo = new EscribirArchivo();
  39.         String contenido = generarNumeros();
  40.        
  41.         archivo.crearArchivoDeTexto();
  42.         archivo.escribirArchivo(contenido);
  43.     }
  44.  
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement