> 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/ud5-entrada-e-saida-da-informacion/ficheiros/clase-file.md).

# Clase File

A clase `File` permite crear, eliminar e obter información sobre ficheiros e directorios.

{% hint style="warning" %}
**Documentación oficial:** <https://docs.oracle.com/javase/7/docs/api/java/io/File.html>
{% endhint %}

## Métodos de File

| Método                               | Descrición                                                    |
| ------------------------------------ | ------------------------------------------------------------- |
| `boolean createNewFile()`            | Crea un ficheiro novo.                                        |
| `boolean exists()`                   | Comproba se o ficheiro existe.                                |
| `boolean delete()`                   | Elimina un ficheiro ou un directorio.                         |
| `long length()`                      | Obtén o tamaño do ficheiro en bytes.                          |
| `String getName()`                   | Obtén o nome do ficheiro.                                     |
| `String getAbsolutePath()`           | Obtén a ruta absoluta do ficheiro.                            |
| `boolean isDirectory()`              | Comproba se é un directorio.                                  |
| `boolean isFile()`                   | Comproba se é un ficheiro.                                    |
| `File[] listFiles()`                 | Lista os ficheiros e directorios contidos nun directorio.     |
| `String[] list()`                    | Igual que `listFiles()`, pero devolvendo nomes como `String`. |
| `boolean canWrite()`                 | Comproba se é posible escribir no ficheiro.                   |
| `boolean canRead()`                  | Comproba se é posible ler do ficheiro.                        |
| `boolean mkdir()`                    | Crea un directorio co nome do obxecto `File`.                 |
| `boolean renameToFile(File newName)` | Renomea o ficheiro.                                           |
| `String getParent()`                 | Devolve o nome do directorio pai.                             |

## Crear e obter información de un ficheiro

```java
import java.io.File;
import java.io.IOException;

public class ExemploFile {
    public static void main(String[] args) {
        try {
            String path = "C:\\Users\\...\\Desktop\\";
            String nombre = "PruebaAD.txt";
            File fichero = new File(path + nombre);
            if (fichero.createNewFile()) {
                System.out.println("Fichero creado: " + fichero.getName());
            } else {
                System.out.println("El fichero ya existe");
            }
            System.out.println("Ruta: " + fichero.getAbsolutePath());
            System.out.println("Tamaño: " +  fichero.length() + "Bytes");
            System.out.println("¿Es Fichero?: " +  fichero.isFile());
            System.out.println("El fichero existe: " +  fichero.exists());
            System.out.println("¿Se puede leer?: " +  fichero.canRead());
            System.out.println("¿Se puede escribir?: " + fichero.canWrite());
            if (fichero.delete()){
                System.out.println("Fichero borrado correctamente");
            } else {
                System.out.println("No se ha podido borrar el fichero");
            }

        } catch (IOException e){
            System.out.println("Error al crear el fichero");
        }
    }
}
```


---

# 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/ud5-entrada-e-saida-da-informacion/ficheiros/clase-file.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.
