Arquivos Java
O manuseio de arquivos é uma parte importante de qualquer aplicativo.
Java tem vários métodos para criar, ler, atualizar e excluir arquivos.
Manipulação de arquivos Java
A File
classe do java.io
pacote, nos permite trabalhar com arquivos.
Para usar a File
classe, crie um objeto da classe e especifique o nome do arquivo ou do diretório:
Exemplo
import java.io.File; // Import the File class
File myObj = new File("filename.txt"); // Specify the filename
Se você não sabe o que é um pacote, leia nosso Tutorial de Pacotes Java .
A File
classe tem muitos métodos úteis para criar e obter informações sobre arquivos. Por exemplo:
Method | Type | Description |
---|---|---|
canRead() |
Boolean | Tests whether the file is readable or not |
canWrite() |
Boolean | Tests whether the file is writable or not |
createNewFile() |
Boolean | Creates an empty file |
delete() |
Boolean | Deletes a file |
exists() |
Boolean | Tests whether the file exists |
getName() |
String | Returns the name of the file |
getAbsolutePath() |
String | Returns the absolute pathname of the file |
length() |
Long | Returns the size of the file in bytes |
list() |
String[] | Returns an array of the files in the directory |
mkdir() |
Boolean | Creates a directory |
Você aprenderá como criar, escrever, ler e excluir arquivos nos próximos capítulos: