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

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

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列字符字符串。

英文:
The following 5 columns of character strings are stored as text file separated by tab code.
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
...
    
List&lt;List&lt;String&gt;&gt; abc = new ArrayList&lt;&gt;();
List&lt;String&gt; data = new ArrayList&lt;String&gt;();try(
BufferedReader fileContent = new BufferedReader(new InputStreamReader(file)))
{
	while ((strLine = fileContent.readLine()) != null) {
		List&lt;String&gt; line = new LinkedList&lt;&gt;(Arrays.asList(strLine.split(&quot;\r?\n|\r&quot;)));
		// abc.add(r1c1 = filename1 r1c2 = abc1 r1c3 = def1 r1c4 = ghi1 r1c5 = col51) as
		// list value then access r1c1 = filename1 as a value outside the loop
		for (int i = 0; i &lt; line.size(); i++) {
			data = Arrays.asList(strLine.split(&quot;\t&quot;));
			abc.add(aliveData);
		}
	}
}

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

答案1

得分: -1

public static void main(String[] args) throws FileNotFoundException {
    File file = new File("D:\\testout.txt");
    InputStream in = new FileInputStream(file);
    List<List<String>> splitByNewLine = convertInputStreamToString(in);
    for(List<String> data : splitByNewLine) {
        System.out.println(data);
    }
}

private static List<List<String>> convertInputStreamToString(InputStream file) {

    List<List<String>> fileData = new ArrayList<>();
    List<String> data = new ArrayList<String>();

    try (BufferedReader fileContent = new BufferedReader(new InputStreamReader(file))) {
        String strLine = null;
        while ((strLine = fileContent.readLine()) != null) {
            data = Arrays.asList(strLine.split("\t"));
            fileData.add(data);
        }
    } catch (IOException e) {
        System.out.println("InputError: %s" + e.getMessage());
    }
    return fileData;
}

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]


<details>
<summary>英文:</summary>

    public static void main(String[] args) throws FileNotFoundException {
    		File file = new File(&quot;D:\\testout.txt&quot;);
    		InputStream in = new FileInputStream(file);
    		List&lt;List&lt;String&gt;&gt; splitByNewLine = convertInputStreamToString(in);
    		for(List&lt;String&gt; data : splitByNewLine) {
    			System.out.println(data);
    		}
    	}
    
    	private static List&lt;List&lt;String&gt;&gt; convertInputStreamToString(InputStream file) {
    
    		List&lt;List&lt;String&gt;&gt; fileData = new ArrayList&lt;&gt;();
    		List&lt;String&gt; data = new ArrayList&lt;String&gt;();
    
    		try (BufferedReader fileContent = new BufferedReader(new InputStreamReader(file))) {
    			String strLine = null;
    			while ((strLine = fileContent.readLine()) != null) {
    				data = Arrays.asList(strLine.split(&quot;\t&quot;));
    				fileData.add(data);
    			}
    		} catch (IOException e) {
    			System.out.println(&quot;InputError: %s&quot; + e.getMessage());
    		}
    		return fileData;
    	}

    
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]`

</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:

确定