File Java


La gestione dei file è una parte importante di qualsiasi applicazione.

Java ha diversi metodi per creare, leggere, aggiornare ed eliminare file.


Gestione dei file Java

La Fileclasse del java.iopacchetto ci consente di lavorare con i file.

Per utilizzare la Fileclasse, crea un oggetto della classe e specifica il nome del file o della directory:

Esempio

import java.io.File;  // Import the File class

File myObj = new File("filename.txt"); // Specify the filename

Se non sai cos'è un pacchetto, leggi il nostro Tutorial sui pacchetti Java .

La Fileclasse ha molti metodi utili per creare e ottenere informazioni sui file. Per esempio:

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

Imparerai come creare, scrivere, leggere ed eliminare file nei prossimi capitoli: