0

我有一个文件名列表(filelist),我想将其检索并存储到 File 数组(files)中。

但我不知道如何读取文件并存储到其中。意味着我喜欢将文件列表中的文件添加到文件中。我怎样才能做到这一点?

public static File[] files; 
private List<String> filelist;
File targetDirector = new File(targetPath);
files = targetDirector.listFiles();  
/* This will get all files in that directory, 
   but I just want the file listed in filelist */
4

2 回答 2

0
files = targetDirector.listFiles(); 

这一行将为您提供该路径目录包含的文件列表。

但我不知道如何读取文件并存储到其中。

你可以像这样阅读

 for (int i=0 ; i< files.length ; i ++)
  {
     //To Read 

     BufferedReader bufferedReader = new BufferedReader(new FileReader( file[i] );
     String read;
     StringBuilder builder = new StringBuilder("");

     while((read = bufferedReader.readLine()) != null){
            builder.append(read);
          }
     Log.d("File Out", builder.toString());
     bufferedReader.close();


    // To Store file 

    BufferedWriter bufferedWriter = new BufferedWriter(file[i]);
    bufferedWriter.write("File is writing..... ");
    bufferedWriter.close();

 }
于 2013-04-10T06:53:13.367 回答
0

我认为这种方式更好

                   files = targetDirector.listFiles(
                    new FilenameFilter() {
                     @Override public boolean accept(File dir, String name) {
                         //mspoke is List containing name of file needed to read
                         if (mSpoke.contains(name)){

                            return true; 
                         }else{
                             return false;
                         }
                     }
                   }

                   );
于 2013-04-22T06:40:53.990 回答