英文:
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
List
try (BufferedReader fileContent = new BufferedReader(new InputStreamReader(file))) {
while ((strLine = fileContent.readLine()) != null) {
List
// 将(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<List<String>> abc = new ArrayList<>();
List<String> data = new ArrayList<String>();try(
BufferedReader fileContent = new BufferedReader(new InputStreamReader(file)))
{
	while ((strLine = fileContent.readLine()) != null) {
		List<String> line = new LinkedList<>(Arrays.asList(strLine.split("\r?\n|\r")));
		// 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 < line.size(); i++) {
			data = Arrays.asList(strLine.split("\t"));
			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("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>
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论