4个回答
展开全部
/**
* 列出文件夹下的子文件夹名
* @param localRoot
* @throws content
*/
public static void list(String localRoot) throws Exception {
File[] fs = new File(localRoot).listFiles();
if ((fs == null) || (fs.length <= 0)) {
System.out.println("空文件夹");
return;
}
for (File f : fs) {
if (f.isDirectory()) {
System.out.println("目录:"+ f.getName());
}
}
}
建立个class然后见个main方法调用一下就可以了!
* 列出文件夹下的子文件夹名
* @param localRoot
* @throws content
*/
public static void list(String localRoot) throws Exception {
File[] fs = new File(localRoot).listFiles();
if ((fs == null) || (fs.length <= 0)) {
System.out.println("空文件夹");
return;
}
for (File f : fs) {
if (f.isDirectory()) {
System.out.println("目录:"+ f.getName());
}
}
}
建立个class然后见个main方法调用一下就可以了!
展开全部
import java.io.*;
public class Test
{
static void myList(String s)
{
File f=new File(s);
String[] m=null;
m=f.list();
for(int i=0;i<m.length;i++)
{
File fm=new File(s+"/"+m[i]);
if(!fm.isFile())
{
System.out.println (fm.getName());
myList(s+"/"+m[i]);
}
}
}
public static void main (String[] args) {
myList("a");//假设你的目录名是a 你可以换成别的
}
}
public class Test
{
static void myList(String s)
{
File f=new File(s);
String[] m=null;
m=f.list();
for(int i=0;i<m.length;i++)
{
File fm=new File(s+"/"+m[i]);
if(!fm.isFile())
{
System.out.println (fm.getName());
myList(s+"/"+m[i]);
}
}
}
public static void main (String[] args) {
myList("a");//假设你的目录名是a 你可以换成别的
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
递归调用就行了嘛
public class Test {
public static void find(String filePath) {
File file = new File(filePath);
if (file.isDirectory()) {
System.out.println(file.getPath());
File[] listFiles = file.listFiles();
if (listFiles != null) {
for (int i = 0; i < listFiles.length; i++) {
if (listFiles[i].isDirectory()) {
Test.find(listFiles[i].getPath());
}
}
}
}
}
public static void main(String[] args) {
find("c:");
}
}
public class Test {
public static void find(String filePath) {
File file = new File(filePath);
if (file.isDirectory()) {
System.out.println(file.getPath());
File[] listFiles = file.listFiles();
if (listFiles != null) {
for (int i = 0; i < listFiles.length; i++) {
if (listFiles[i].isDirectory()) {
Test.find(listFiles[i].getPath());
}
}
}
}
}
public static void main(String[] args) {
find("c:");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-01-05
展开全部
//:ListDirectories.java
import java.io.File;
import java.io.FileFilter;
public class ListDirectories {
//File filter object.
private static FileFilter filter = new MyFilter();
public static void main(String[] args) {
String dir = javax.swing.JOptionPane.showInputDialog("Input root dir here:");
if(dir!=null)
printRecursive(dir);
}
private static void printRecursive(String dir) {
File df = new File(dir);
if(df.isDirectory() && df.canRead())
printRecursive(df);
else
System.out.println("Directory '"+dir+"' is not exists or can not be read.");
}
private static void printRecursive(File dir){
File[] files = dir.listFiles(filter);
if(files!=null)
for(int i=0; i<files.length; i++){
System.out.println(files[i].getAbsolutePath());
if(files[i].canRead()){
printRecursive(files[i]);
}
}
}
//File filter
private static class MyFilter implements FileFilter{
public boolean accept(File pathname) {
return pathname.isDirectory();
}
}
}
import java.io.File;
import java.io.FileFilter;
public class ListDirectories {
//File filter object.
private static FileFilter filter = new MyFilter();
public static void main(String[] args) {
String dir = javax.swing.JOptionPane.showInputDialog("Input root dir here:");
if(dir!=null)
printRecursive(dir);
}
private static void printRecursive(String dir) {
File df = new File(dir);
if(df.isDirectory() && df.canRead())
printRecursive(df);
else
System.out.println("Directory '"+dir+"' is not exists or can not be read.");
}
private static void printRecursive(File dir){
File[] files = dir.listFiles(filter);
if(files!=null)
for(int i=0; i<files.length; i++){
System.out.println(files[i].getAbsolutePath());
if(files[i].canRead()){
printRecursive(files[i]);
}
}
}
//File filter
private static class MyFilter implements FileFilter{
public boolean accept(File pathname) {
return pathname.isDirectory();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询