Txerrinko

Ejercicio 3 tanda 2 tema 7 I/O

May 12th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. package Tanda2;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8.  
  9. public class Ejercicio3Tema7 {
  10.  
  11.     /**
  12.      * @param args
  13.      */
  14.     public static void main(String[] args) {
  15.         /*
  16.          *
  17.          * crear un fihero de texto con lod strings que se van solicitando al
  18.          * usuario cada string se copiara al fichero en una nueva linea cada
  19.          * string se copiara invertido c cada linea de fichero tendra un maximo
  20.          * de 30 caracteres si tiene mas , recortara por el final }
  21.          */
  22.         BufferedWriter fb = null;
  23.         BufferedReader br;
  24.         String cad;
  25.         String str = "";
  26.        
  27.        
  28.        
  29.         try {
  30.            
  31.            
  32.            
  33.            
  34.             fb = new BufferedWriter(new FileWriter("juve.txt"));
  35.             br = new BufferedReader(new InputStreamReader(System.in));
  36.            
  37.             System.out.println("Introduce frase");
  38.             cad=Consola.leeString();
  39.            
  40.            
  41.             //generamos el String para que vaya leyendolo linea por linea
  42.             cad = br.readLine();
  43.             br.close();
  44.             int c = 0;
  45.             int maxLong = 30;
  46.             for (int i = cad.length() - 1; i >= 0; i--) {
  47.                 // i obitnene la longitud de cad y mientras esta sea mayor o
  48.                 // igual
  49.                 // que 0
  50.                 // y el contador del buffer sea menor o igual que 30 , i ira
  51.                 // decrementando
  52.                 str += cad.charAt(i);
  53.                 c++;
  54.                 if (c >= maxLong) {
  55.                    
  56.                     //si c es mayor que su longitud  maxima
  57.                     //escribir el sttring que hemos recibido por teclado
  58.                     // lo pasamos  a una nuva linea
  59.                     // y restauramos sus valores por defecto, dejandolos vacios
  60.                    
  61.                     fb.write(str);
  62.                     fb.newLine();
  63.                     c = 0;
  64.                     str = "";
  65.                 }
  66.                 // se ira almacenando los caracteres de xada posicion i en el
  67.                 // buffer
  68.                 // mientras c vaya aumentado e i disminuyendo
  69.             }
  70.             if (c > 0) {
  71.                 fb.write(str);
  72.                 fb.newLine();
  73.             }
  74.         } catch (IOException e) {
  75.             e.printStackTrace();
  76.         } finally {
  77.             try {
  78.                 fb.flush();
  79.                 fb.close();
  80.             } catch (IOException e) {
  81.                 e.printStackTrace();
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment