获取扩展名为文件夹及其子文件夹中所有文件的列表

时间:2013-05-30 09:44:27

标签: java android

我正在尝试获取文件夹及其子文件夹中扩展名为“.mp3”的文件列表,但该函数仅在根文件夹中返回.mp3文件。

我需要一个像这样的结构

http://s1.ipicture.ru/uploads/20130530/Kiju7Bf7.png 源代码:

private ListView lvFolders;
private File file;
private List<String> myList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.folder, null);
    lvFolders = (ListView) v.findViewById(R.id.lvFolders);

    myList = new ArrayList<String>();

    String root_sd = Environment.getExternalStorageDirectory().toString();
    file = new File(root_sd);
    addToSongs(file);

    /*File list[] = file.listFiles();
    for (int i = 0; i < list.length; i++) {
        myList.add(list[i].getName());
    }*/

    lvFolders.setAdapter(new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, myList));

    lvFolders.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            File temp_file = new File(file, myList.get(position));

            if (!temp_file.isFile()) {
                file = new File(file, myList.get(position));
                File list[] = file.listFiles();

                myList.clear();

                for (int i = 0; i < list.length; i++) {
                    myList.add(list[i].getName());
                }
                Toast.makeText(getActivity(), file.toString(),
                        Toast.LENGTH_LONG).show();
                lvFolders.setAdapter(new ArrayAdapter<String>(
                        getActivity(), android.R.layout.simple_list_item_1,
                        myList));
            }
        }
    });

    return v;

}

FilenameFilter textFilter = new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3"));
    }
};

private void addToSongs(File home) {
    for (File file : home.listFiles(textFilter)) {
        if (file.isDirectory()) {
            addToSongs(file);
        } else {
            myList.add(file.getAbsolutePath());
        }
    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:0)

试试这个

    import java.io.File;

    public class ListFiles 
    {

     public static void main(String[] args) 
    {

      // Directory path here
      String path = "."; 

      String files;
      File folder = new File(path);
      File[] listOfFiles = folder.listFiles(); 

      for (int i = 0; i < listOfFiles.length; i++) 
      {

       if (listOfFiles[i].isFile()) 
       {
       files = listOfFiles[i].getName();
       System.out.println(files);
          }
      }
    }

}

要获取文件夹中的所有文件,如果您想要特定的文件扩展名,请使用此

import java.io.File;

public class ListFiles 
{

 public static void main(String[] args) 
{

  // Directory path here
  String path = "."; 

  String files;
  File folder = new File(path);
  File[] listOfFiles = folder.listFiles(); 

  for (int i = 0; i < listOfFiles.length; i++) 
  {

   if (listOfFiles[i].isFile()) 
   {
   files = listOfFiles[i].getName();
       if (files.endsWith(".txt") || files.endsWith(".TXT"))
       {
          System.out.println(files);
        }
     }
  }
}
}

答案 1 :(得分:0)

此代码可能有所帮助:

public static void main(String[] args) 
{
    File file = new File("C:/MyDirectory");
    List<File> list = new ArrayList<File>();
    getMp3Files(file, list);
    // Use list which contains the files with .mp3 extension
}

public static void getMp3Files(File rootfile, List<File> list)
{       
    File[] files; 
    if(rootFile.isDirectory())
    {   
        files = rootfile.listFiles();
    }
    else
        return;
    for (File file : files)
    {
        if(file.isDirectory())
        {
            getMp3Files(file, list);
        }
        else
        {
            if(file.getName().endsWith(".mp3"))
            {
                list.add(file);
            }
        }
    }

}