> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/exercicios-java/ud5-solucions/solucions-bufferedreader-e-bufferedwriter.md).

# Solucións BufferedReader e BufferedWriter

1. lectura por pantalla

```java
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class EscrituraPantalla {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String nombreFichero = "LecturaPantalla.txt";

        System.out.println("Introduce texto. Pulsa Enter en línea vacía para terminar:");

        try (FileWriter writer = new FileWriter(nombreFichero)) {
            while (true) {
                String linea = sc.nextLine();
                if (linea.isEmpty()) {
                    break; // termina si la línea está vacía
                }
                writer.write(linea + "\n"); // escribir línea en el fichero
            }
            System.out.println("Texto guardado correctamente en " + nombreFichero);
        } catch (IOException e) {
            System.out.println("Error al escribir el fichero:");
            e.printStackTrace();
        }

        sc.close();
    }
}

```

2. Invertir Palabras

```java
package Invertir;

import java.io.*;

public class Invertir {

        public static void main(String[] args) {

            String ficheroEntrada = "C:\\Users\\iagom\\Desktop\\Entrada.txt";
            String ficheroSalida = "C:\\Users\\iagom\\Desktop\\Salida.txt";

            int numLineas = 0;
            //Contamos las lineas del fichero
            try (BufferedReader contador = new BufferedReader(new FileReader(ficheroEntrada))) {
                while (contador.readLine() != null) {
                        numLineas++;
                }
            } catch (Exception e){
                System.out.println("Error en la lectura del fichero:" + e.getMessage());
            }


            String[][] contenido = new String[numLineas][];
            //Pasar cadenas a un array
            try (BufferedReader lector = new BufferedReader(new FileReader(ficheroEntrada))) {
                String linea;
                int indice = 0;

                while ((linea = lector.readLine()) != null) {
                    contenido[indice] = linea.split(" ");
                    indice++;
                }
            } catch (IOException e){
                System.out.println("Error leyendo las palabras");
            }


            try (BufferedWriter escritor = new BufferedWriter(new FileWriter(ficheroSalida))) {
            //Escribir lineas
                for (int i = contenido.length - 1; i >= 0; i--) {
                    for (int j = contenido[i].length - 1; j >= 0; j--) {
                        escritor.write(contenido[i][j]);
                        escritor.write(" ");
                    }
                    escritor.newLine();
                }
            } catch (IOException e){
                System.out.println("Error en la escritura del fichero");
            }

            System.out.println("El programa ha finalizado correctamente.");

        }
}


```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/exercicios-java/ud5-solucions/solucions-bufferedreader-e-bufferedwriter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
