Questa classe stampa su file tutti i file contenuti in una cartella considerando anche le varie ramificazioni della stasse.
Se ti può essere utile!!!
codice:
import java.io.*;
import javax.swing.JOptionPane;
public class RicercaFile {
public static void main( String[] args )
{
String path = JOptionPane.showInputDialog( null,"Inserisci il path", "Input path", JOptionPane.INFORMATION_MESSAGE );
if( ( path == null ) ||( path.equals( "" ) ) )
{
JOptionPane.showMessageDialog( null,"Dovevi inserire un path", "Errore", JOptionPane.ERROR_MESSAGE );
System.exit( 0 );
}
try{
String fw = "c:\\LogFile.txt";
PrintStream scrivi = new PrintStream(new FileOutputStream(fw));
mostra(path,scrivi);
scrivi.close();
} catch(IOException e){}
System.exit( 1 );
}
public static void mostra(String path, PrintStream scrivi)
{
try{
File f = new File( path );
File[] contenuto = f.listFiles();
if(contenuto.length != 0)
{
for( int i = 0; i < contenuto.length; i++ )
{
if( contenuto[i].isDirectory() )
{
mostra(contenuto[i].getPath(),scrivi);
}
}
for( int i = 0; i < contenuto.length; i++ )
{
if( contenuto[i].isFile() )
{
String dim = contenuto[i].getPath();
System.out.println(contenuto[i].getPath() + ";" + contenuto[i].length()/1024 + ";" + dim.length() );
scrivi.println(contenuto[i].getPath() + ";" + contenuto[i].length()/1024 + ";" + dim.length());
}
}
}
else
{
JOptionPane.showMessageDialog( null, path + ": non ha files", "Attenzione", JOptionPane.INFORMATION_MESSAGE );
}
} catch(Exception e)
{
JOptionPane.showMessageDialog( null, path + ": Exception: " + e, "Attenzione", JOptionPane.INFORMATION_MESSAGE );
}
}
}