import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.LinkedList; import java.util.List; /** * @author: Andrea Michienzi */ public class EsercizioIO { public static void main(String[] args) throws IOException { String basedir="/path/to/dir/"; File startDirectory=new File(basedir); if(!startDirectory.isDirectory()) { System.out.println("Initial file is not a directory!"); return; } List directories=new LinkedList(); directories.add(startDirectory); DataOutputStream d=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("directories")))); DataOutputStream f=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("files")))); while(directories.size()>0) { File[] filesInCurrentDirectory=directories.remove(0).listFiles(); for(File ficd : filesInCurrentDirectory) { String towrite=ficd.getName().toString() + "\n"; if(ficd.isDirectory()) { d.write(towrite.getBytes()); directories.add(ficd); } else { f.write(towrite.getBytes()); } } } d.flush(); f.flush(); d.close(); f.close(); } }