Как мы можем прочитать файл построчно и получить доступ к значениям из списка для ниже спецификации в java - PullRequest
0 голосов
/ 07 апреля 2020
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);
        }
    }
}

Как следующие 5 столбцов строк символов можно сохранить в виде текстового файла, разделенного кодом табуляции.

Ответы [ 2 ]

0 голосов
/ 07 апреля 2020
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;
    }

ВЫХОД: [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]

0 голосов
/ 07 апреля 2020

Вы не получаете требуемый результат из-за внутреннего l oop, который не нужен. Сделайте это следующим образом:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<ArrayList<String>> abc = new ArrayList<ArrayList<String>>();
        String strLine;
        try (BufferedReader fileContent = new BufferedReader(new FileReader("file.txt"))) {
            while ((strLine = fileContent.readLine()) != null) {
                abc.add(new ArrayList<>(Arrays.asList(strLine.split("\r?\n|\r"))));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Display the data
        for (ArrayList<String> list : abc) {
            System.out.println(list);
        }
    }
}

Вывод:

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

Не стесняйтесь комментировать в случае каких-либо сомнений / проблем.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...