读取文件逐行处理,并在Java中按照以下规范访问List> 中的值。

huangapple go评论103阅读模式
英文:

Read file line by line and Access values from List<List<String>> for below specifications in java

问题

以下5列字符字符串以制表符分隔的方式存储在文本文件中。
r1c1 = 文件名1 r1c2 = abc1 r1c3 = def1 r1c4 = ghi1 r1c5 = 列51
r2c1 = 文件名2 r2c2 = abc2 r2c3 = def2 r2c4 = ghi2 r2c5 = 列52
r3c1 = 文件名3 r3c2 = abc3 r3c3 = def3 r3c4 = ghi3 r3c5 = 列53
r4c1 = 文件名4 r4c2 = abc4 r4c3 = def4 r4c4 = ghi4 r4c5 = 列54
...

List<List> abc = new ArrayList<>();
List data = new ArrayList();
try (BufferedReader fileContent = new BufferedReader(new InputStreamReader(file))) {
while ((strLine = fileContent.readLine()) != null) {
List line = new LinkedList<>(Arrays.asList(strLine.split("\r?\n|\r")));
// 将(r1c1 = 文件名1 r1c2 = abc1 r1c3 = def1 r1c4 = ghi1 r1c5 = 列51)作为列表值添加,然后在循环外部访问r1c1 = 文件名1作为值
for (int i = 0; i < line.size(); i++) {
data = Arrays.asList(strLine.split("\t"));
abc.add(aliveData);
}
}
}
获取以制表符分隔的文本文件中的5列字符字符串。

英文:
  1. The following 5 columns of character strings are stored as text file separated by tab code.
  2. r1c1 = filename1 r1c2 = abc1 r1c3 = def1 r1c4 = ghi1 r1c5 = col51
  3. r2c1 = filename2 r2c2 = abc2 r2c3 = def2 r2c4 = ghi2 r2c5 = col52
  4. r3c1 = filename3 r3c2 = abc3 r3c3 = def3 r3c4 = ghi3 r3c5 = col53
  5. r4c1 = filename4 r4c2 = abc4 r4c3 = def4 r4c4 = ghi4 r4c5 = col54
  6. ...
  7. List&lt;List&lt;String&gt;&gt; abc = new ArrayList&lt;&gt;();
  8. List&lt;String&gt; data = new ArrayList&lt;String&gt;();try(
  9. BufferedReader fileContent = new BufferedReader(new InputStreamReader(file)))
  10. {
  11. while ((strLine = fileContent.readLine()) != null) {
  12. List&lt;String&gt; line = new LinkedList&lt;&gt;(Arrays.asList(strLine.split(&quot;\r?\n|\r&quot;)));
  13. // abc.add(r1c1 = filename1 r1c2 = abc1 r1c3 = def1 r1c4 = ghi1 r1c5 = col51) as
  14. // list value then access r1c1 = filename1 as a value outside the loop
  15. for (int i = 0; i &lt; line.size(); i++) {
  16. data = Arrays.asList(strLine.split(&quot;\t&quot;));
  17. abc.add(aliveData);
  18. }
  19. }
  20. }

Get the 5 columns of character strings that are stored as text file separated by tab code.

答案1

得分: -1

  1. public static void main(String[] args) throws FileNotFoundException {
  2. File file = new File("D:\\testout.txt");
  3. InputStream in = new FileInputStream(file);
  4. List<List<String>> splitByNewLine = convertInputStreamToString(in);
  5. for(List<String> data : splitByNewLine) {
  6. System.out.println(data);
  7. }
  8. }
  9. private static List<List<String>> convertInputStreamToString(InputStream file) {
  10. List<List<String>> fileData = new ArrayList<>();
  11. List<String> data = new ArrayList<String>();
  12. try (BufferedReader fileContent = new BufferedReader(new InputStreamReader(file))) {
  13. String strLine = null;
  14. while ((strLine = fileContent.readLine()) != null) {
  15. data = Arrays.asList(strLine.split("\t"));
  16. fileData.add(data);
  17. }
  18. } catch (IOException e) {
  19. System.out.println("InputError: %s" + e.getMessage());
  20. }
  21. return fileData;
  22. }

OUTPUT:
[r1c1 = filename1, r1c2 = abc1, r1c3 = def1, r1c4 = ghi1, r1c5 = col51] [r2c1 = filename2, r2c2 = abc2, r2c3 = def2, r2c4 = ghi2, r2c5 = col52] [r3c1 = filename3, r3c2 = abc3, r3c3 = def3, r3c4 = ghi3, r3c5 = col53] [r4c1 = filename4, r4c2 = abc4, r4c3 = def4, r4c4 = ghi4, r4c5 = col54]

  1. <details>
  2. <summary>英文:</summary>
  3. public static void main(String[] args) throws FileNotFoundException {
  4. File file = new File(&quot;D:\\testout.txt&quot;);
  5. InputStream in = new FileInputStream(file);
  6. List&lt;List&lt;String&gt;&gt; splitByNewLine = convertInputStreamToString(in);
  7. for(List&lt;String&gt; data : splitByNewLine) {
  8. System.out.println(data);
  9. }
  10. }
  11. private static List&lt;List&lt;String&gt;&gt; convertInputStreamToString(InputStream file) {
  12. List&lt;List&lt;String&gt;&gt; fileData = new ArrayList&lt;&gt;();
  13. List&lt;String&gt; data = new ArrayList&lt;String&gt;();
  14. try (BufferedReader fileContent = new BufferedReader(new InputStreamReader(file))) {
  15. String strLine = null;
  16. while ((strLine = fileContent.readLine()) != null) {
  17. data = Arrays.asList(strLine.split(&quot;\t&quot;));
  18. fileData.add(data);
  19. }
  20. } catch (IOException e) {
  21. System.out.println(&quot;InputError: %s&quot; + e.getMessage());
  22. }
  23. return fileData;
  24. }
  25. OUTPUT:
  26. `[r1c1 = filename1, r1c2 = abc1, r1c3 = def1, r1c4 = ghi1, r1c5 = col51]
  27. [r2c1 = filename2, r2c2 = abc2, r2c3 = def2, r2c4 = ghi2, r2c5 = col52]
  28. [r3c1 = filename3, r3c2 = abc3, r3c3 = def3, r3c4 = ghi3, r3c5 = col53]
  29. [r4c1 = filename4, r4c2 = abc4, r4c3 = def4, r4c4 = ghi4, r4c5 = col54]`
  30. </details>

huangapple
  • 本文由 发表于 2020年4月7日 09:45:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61071533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定