【问题标题】:how to read the data from the files of a directory如何从目录的文件中读取数据
【发布时间】:2015-03-03 09:29:06
【问题描述】:
    public class FIlesInAFolder {

        private static BufferedReader br;

        public static void main(String[] args) throws IOException {
            File folder = new File("C:/filesexamplefolder");
            FileReader fr = null;

            if (folder.isDirectory()) {
                for (File fileEntry : folder.listFiles()) {
                    if (fileEntry.isFile()) {
                        try {
                            fr = new FileReader(folder.getAbsolutePath() + "\\" + fileEntry.getName());
                            br = new BufferedReader(fr);
System.out.println(""+br.readLine());
                        }
                        catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        finally {
                            br.close();
                            fr.close();
                        }
                    }
                }
            }
        }
    }

如何打印目录的第一个文件的第一个单词,第二个文件的第二个单词和同一目录的第三个文件的第三个单词。

i am able to open directory and print the line from each file of the directory,
but tell me how to print the first word from first file and second word from second file and so on . . 

【问题讨论】:

  • 从一个目录中读取数据并不容易...
  • 为每个文件创建不同的流
  • 你有新的 FileReader(folder.getAbsolutePath()+"\\"+fileEntry.getName()),新的 FIleReader(fileEntry) 会这样做
  • 也完整阅读了邮件正文

标签: java


【解决方案1】:

类似下面的内容将从第一个文件中读取第一个单词,从第二个文件中读取第二个单词,...从第 n 个文件中读取第 n 个单词。您可能需要做一些额外的工作来提高代码的稳定性。

import java.io.File;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;

public class SOAnswer {

    private static void printFirst(File file, int offset) throws FileNotFoundException, IOException {

        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);

        String line = null;
        while ( (line = br.readLine()) != null ) {
            String[] split = line.split(" ");
            if(split.length >= offset) {
                String targetWord = split[offset];
            } 
            // we do not care if files are read that do not match your requirements, or 
            // for reading complete files as you only care for the first word
            break;
        }

        br.close();
        fr.close(); 
    }

    public static void main(String[] args) throws Exception  {
         File folder = new File(args[0]);
         if(folder.isDirectory()) {
             int offset = 0;
             for(File fileEntry : folder.listFiles()) {
                 if(fileEntry.isFile()) {
                     printFirst(fileEntry, offset++); // handle exceptions if you wish
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2013-02-03
    • 1970-01-01
    • 2015-04-22
    • 2022-01-14
    • 2019-08-12
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多