3

我身边有100 files in a folder。我正在尝试阅读所有这些文件one by one。每个文件都有这样的数据,每一行都类似于一个用户 ID。

960904056
6624084
1096552020
750160020
1776024
211592064
1044872088
166720020
1098616092
551384052
113184096
136704072

所以我需要逐行读取该文件,然后将每个用户 ID 存储在LinkedHashSet. 我可以使用以下代码从特定文件夹中读取所有文件。但是使用我编写的以下 java 代码,我不确定如何逐行读取这些文件,然后将每个用户 id 存储在LinkedHashSet?

public static void main(String args[]) {

    File folder = new File("C:\\userids-20130501");
    File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
        File file = listOfFiles[i];
        if (file.isFile() && file.getName().endsWith(".txt")) {
            try {
                String content = FileUtils.readFileToString(file);
                System.out.println(content);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

任何帮助将不胜感激?还有更好的方法来做同样的过程吗?

4

4 回答 4

3

由于您使用的是FileUtils. 该类具有readLines()返回列表的方法String

然后,您可以使用该方法将其添加List到 String 中。LinkedHashSetaddAll()

试试这个 -

public static void main(String args[]) {
File folder = new File("C:\\userids-20130501");
File[] listOfFiles = folder.listFiles();

Set<String> userIdSet = new LinkedHashSet<String>();
for (int i = 0; i < listOfFiles.length; i++) {
    File file = listOfFiles[i];
    if (file.isFile() && file.getName().endsWith(".txt")) {
        try {
            List<String> content = FileUtils.readLines(file, Charset.forName("UTF-8"));
            userIdSet.addAll(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 }
于 2013-06-19T19:03:15.817 回答
1

您可以使用以下readLine()方法BufferedReader逐行读取文件:

public static void main(String args[]) {

File folder = new File("C:\\userids-20130501");
File[] listOfFiles = folder.listFiles();

Set<String> userIdSet = new LinkedHashSet<String>();
for (int i = 0; i < listOfFiles.length; i++) {
    File file = listOfFiles[i];
    if (file.isFile() && file.getName().endsWith(".txt")) {
        try {
          BufferedReader br = new BufferedReader(new FileReader(file));
          String line;
          while((line = br.readLine()) != null) {
              userIdSet.add(line);
           }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 }
}
于 2013-06-19T18:56:03.750 回答
1

如果我理解正确,那么您可以执行以下操作。

// Change generic if you want Integer
Set<String> userIdSet = new LinkedHashSet<String>(); 

// Your code


String content = FileUtils.readFileToString(file);
// Creating array by splitting for every new line content
String[] stn = content.split("\\n");
     for(String xx : stn){
        // parse it as Integer if you changed the generic
        // Adding the file content to Set
        userIdSet.add(xx); 
}
于 2013-06-19T19:08:47.897 回答
1

这是我会做的不同的事情。

  • 对文件使用迭代器

  • 新的 try 语句允许在 try 块中打开“资源”,并在块完成时自动关闭资源。参考

  • continue使用语句排除无效文件。这将删除一层嵌套。看到这个帖子。

当然,所有这些变化都是高度自以为是的。

    public static void main(String[] args) {

       Set<Integer> users = new LinkedHashSet<Integer>();

       File dir = new File("C:\\userids-20130501");
       for (File child : dir.listFiles()) {
         if (!child.isFile() || !child.getName().endsWith(".txt")) {
           continue;
         }

         try (BufferedReader in = new BufferedReader(
             new FileReader(child.getPath()))) {

           String id = null;
           while ((id = in.readLine()) != null) {
             users.add(Integer.parseInt(id));
           }
         } catch (IOException ex) {
           ex.printStackTrace();
         }
       }
   }
于 2013-06-19T19:26:15.020 回答