2013-06-19 7 views
3

我有100 files in a folder。我正試圖讀取所有這些文件one by one。每個文件都有這樣的數據,每行都與用戶標識相似。讀取文件夾中的文件,然後將每個用戶ID存儲在linkedhashset中

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

所以我需要逐行讀取該文件行,然後每個用戶ID存儲在LinkedHashSet。我可以用下面的代碼讀取特定文件夾中的所有文件。但是,我寫下了下面的java代碼,我不知道如何逐行讀取這些文件,然後將每個用戶標識存儲在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(); 
      } 
     } 
    } 
} 

任何幫助將不勝感激呢?還有更好的方法來做同樣的過程?

回答

3

由於您使用FileUtils。該班級有方法readLines(),它返回String的列表。

然後,您可以使用addAll()方法將字符串List添加到LinkedHashSet

試試這個 -

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(); 
     } 
    } 
} 
+0

感謝Junaid的建議。在你的例子中,它會每次創建一個新的LinkedHashSet。我想繼續將用戶標識附加到相同的linkedhashset。 – ferhan

+0

更改了代碼以使用相同的LinkedHashSet。希望這是你想要的。 – JHS

1

您可以使用BufferedReaderreadLine()方法逐行讀取文件一行如下:

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(); 
     } 
    } 
} 
} 
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); 
} 
1

這是我會做的不同的事情。

  • 使用迭代器的文件

  • 新try語句允許在try塊開闢了「資源」,並自動關閉資源塊時完成。 Ref

  • continue語句排除無效文件。這刪除了一層嵌套。 See this post.

所有這些變化都非常固執己見當然。

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(); 
     } 
     } 
    } 
相關問題