Created
May 14, 2012 14:24
-
-
Save ilovejs/2694301 to your computer and use it in GitHub Desktop.
Directories and Files Printer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileFilter; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
public class Dparser { | |
FileWriter out = null; | |
public Dparser(){ | |
try { | |
out = new FileWriter("Idisk.txt"); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
// NodeDao ndao = new NodeDao(); | |
// ndao.show(); | |
File dir = new File("I:"); | |
Dparser p = new Dparser(); | |
p.showall(dir); | |
p.out.close(); | |
} | |
public void showall(File dir) throws IOException{ | |
if(dir.isFile()){ | |
out.write(dir + "\n"); | |
return; | |
} | |
out.write("<<-----------------------Dir " + dir + " has------------------->>\n"); | |
File[] dirbelow = getsubdir(dir); | |
//get file in current directory | |
File[] filebelow = getfiles(dir); | |
if(dirbelow == null || filebelow == null){ | |
return; | |
} | |
//print files in current directory first | |
for (File file : filebelow) { | |
showall(file); | |
} | |
//recursively print anything from sub-directory | |
for (File file : dirbelow) { | |
showall(file); | |
} | |
return; | |
} | |
public File[] getfiles(File dir){ | |
File[] files = null; | |
FileFilter fileFilter = new FileFilter() { | |
public boolean accept(File file) { | |
return (!file.isDirectory()); | |
} | |
}; | |
files = dir.listFiles(fileFilter); | |
return files; | |
} | |
public File[] getsubdir(File dir){ | |
File[] files = null; | |
// This filter only returns directories | |
FileFilter fileFilter = new FileFilter() { | |
public boolean accept(File file) { | |
return file.isDirectory(); | |
} | |
}; | |
files = dir.listFiles(fileFilter); | |
return files; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment