如何将文件和文件夹列表存储在列表中或使用 java 设置?
How to store list of files and folder in list or set using java?
我有 return 代码来检索目录中所有文件路径的列表,但我只获取最后一个文件夹的内容。我有两个文件夹,每个文件夹有3个文件。
这是我的代码:
import java.io.*;
import java.util.*;
class Filedirexts
{
public static void main(String[] args) throws IOException
{
String Dirpath = "E:/Share/tstlpatches";
String fieldirpath ="";
File file = new File(Dirpath);
List<String> strfilelst = new ArrayList<String>();
strfilelst = Filedirexts.getsubdir(file);
System.out.println(strfilelst.size());
for(int i=0;i<strfilelst.size();i++)
{
fieldirpath = strfilelst.get(i);
System.out.println("fieldirpath : "+fieldirpath);
}
}
public static List<String> getsubdir(File file) throws IOException
{
File[] filelist = file.listFiles();
List<String> strfileList = new ArrayList<String>();
System.out.println("filelist" + filelist.length);
for (int i=0; i< filelist.length ; i++)
{
if(filelist[i].exists())
{
if(filelist[i].isFile())
{
file = filelist[i];
System.out.println( " fileeach file : "+fileeach.getAbsolutePath());
strfileList.add(file.getAbsolutePath());
}
else if (filelist[i].isDirectory())
{
file = filelist[i];
System.out.println( " fileeach Directory : "+fileeach.getCanonicalPath());
strfileList = Filedirexts.getsubdir(file);
strfileList.add(file.getCanonicalPath().toString());
}
}
}
return strfileList;
}
}
这是我的文件夹结构:
MainPath E:\Share\tstlpatches 用于代码本身
E:\Share\tstlpatches\BE
E:\Share\tstlpatches\BE\graphical
E:\Share\tstlpatches\BE\graphical\data1.txt
E:\Share\tstlpatches\BE\graphical\data2.txt
E:\Share\tstlpatches\BE\graphical\data3.txt
E:\Share\tstlpatches\BE\test
E:\Share\tstlpatches\BE\test.txt
E:\Share\tstlpatches\BE\test.txt
E:\Share\tstlpatches\BE\test\readme.txt
我只得到
E:\Share\tstlpatches\BE
E:\Share\tstlpatches\BE\test
E:\Share\tstlpatches\BE\test.txt
E:\Share\tstlpatches\BE\test.txt
E:\Share\tstlpatches\BE\test\readme.txt
如果我使用普通方法,它工作正常,但是当我使用列表时,我只得到最后一个文件夹的内容。
我需要做什么才能使代码正常工作?
您的递归方法在每次调用中错误地创建了一个新的 ArrayList<String>
并在每次调用中返回了这个新的 ArrayList
。这就是为什么您只能看到上次调用的内容。
解决此问题的方法如下:
1) 将getsubdir
改为void,将List作为参数传入
public static void getsubdir(File file, List<String> strfileList) throws IOException
{
File[] filelist = file.listFiles();
System.out.println("filelist " + filelist.length);
for (int i=0; i< filelist.length ; i++)
{
if(filelist[i].exists())
{
if(filelist[i].isFile())
{
file = filelist[i];
System.out.println( " fileeach file : "+file.getAbsolutePath());
strfileList.add(file.getAbsolutePath());
}
else if (filelist[i].isDirectory())
{
file = filelist[i];
System.out.println( " fileeach Directory : "+file.getCanonicalPath());
// Note: Since you want the directory first in the list,
// add it before the recursive call
strfileList.add(file.getCanonicalPath().toString());
Filedirexts.getsubdir(file, strfileList);
}
}
}
}
2) 改变你从main中调用它的方式:
而不是:
strfilelst = Filedirexts.getsubdir(file);
只需使用:
Filedirexts.getsubdir(file, strfilelst);
我有 return 代码来检索目录中所有文件路径的列表,但我只获取最后一个文件夹的内容。我有两个文件夹,每个文件夹有3个文件。
这是我的代码:
import java.io.*;
import java.util.*;
class Filedirexts
{
public static void main(String[] args) throws IOException
{
String Dirpath = "E:/Share/tstlpatches";
String fieldirpath ="";
File file = new File(Dirpath);
List<String> strfilelst = new ArrayList<String>();
strfilelst = Filedirexts.getsubdir(file);
System.out.println(strfilelst.size());
for(int i=0;i<strfilelst.size();i++)
{
fieldirpath = strfilelst.get(i);
System.out.println("fieldirpath : "+fieldirpath);
}
}
public static List<String> getsubdir(File file) throws IOException
{
File[] filelist = file.listFiles();
List<String> strfileList = new ArrayList<String>();
System.out.println("filelist" + filelist.length);
for (int i=0; i< filelist.length ; i++)
{
if(filelist[i].exists())
{
if(filelist[i].isFile())
{
file = filelist[i];
System.out.println( " fileeach file : "+fileeach.getAbsolutePath());
strfileList.add(file.getAbsolutePath());
}
else if (filelist[i].isDirectory())
{
file = filelist[i];
System.out.println( " fileeach Directory : "+fileeach.getCanonicalPath());
strfileList = Filedirexts.getsubdir(file);
strfileList.add(file.getCanonicalPath().toString());
}
}
}
return strfileList;
}
}
这是我的文件夹结构: MainPath E:\Share\tstlpatches 用于代码本身
E:\Share\tstlpatches\BE
E:\Share\tstlpatches\BE\graphical
E:\Share\tstlpatches\BE\graphical\data1.txt
E:\Share\tstlpatches\BE\graphical\data2.txt
E:\Share\tstlpatches\BE\graphical\data3.txt
E:\Share\tstlpatches\BE\test
E:\Share\tstlpatches\BE\test.txt
E:\Share\tstlpatches\BE\test.txt
E:\Share\tstlpatches\BE\test\readme.txt
我只得到
E:\Share\tstlpatches\BE
E:\Share\tstlpatches\BE\test
E:\Share\tstlpatches\BE\test.txt
E:\Share\tstlpatches\BE\test.txt
E:\Share\tstlpatches\BE\test\readme.txt
如果我使用普通方法,它工作正常,但是当我使用列表时,我只得到最后一个文件夹的内容。
我需要做什么才能使代码正常工作?
您的递归方法在每次调用中错误地创建了一个新的 ArrayList<String>
并在每次调用中返回了这个新的 ArrayList
。这就是为什么您只能看到上次调用的内容。
解决此问题的方法如下:
1) 将getsubdir
改为void,将List作为参数传入
public static void getsubdir(File file, List<String> strfileList) throws IOException
{
File[] filelist = file.listFiles();
System.out.println("filelist " + filelist.length);
for (int i=0; i< filelist.length ; i++)
{
if(filelist[i].exists())
{
if(filelist[i].isFile())
{
file = filelist[i];
System.out.println( " fileeach file : "+file.getAbsolutePath());
strfileList.add(file.getAbsolutePath());
}
else if (filelist[i].isDirectory())
{
file = filelist[i];
System.out.println( " fileeach Directory : "+file.getCanonicalPath());
// Note: Since you want the directory first in the list,
// add it before the recursive call
strfileList.add(file.getCanonicalPath().toString());
Filedirexts.getsubdir(file, strfileList);
}
}
}
}
2) 改变你从main中调用它的方式:
而不是:
strfilelst = Filedirexts.getsubdir(file);
只需使用:
Filedirexts.getsubdir(file, strfilelst);