简体   繁体   English

如何从txt文件中读取2行

[英]How Can I Read 2 lines From txt File

Hey Guys My I am Dealing With This Issue I Want To merge to line from txt file and show them in Arraylist So My Code Is That And I Want To Skip The -- Line To Show in Arraylist..嘿伙计们,我正在处理这个问题,我想从 txt 文件合并到行并在 Arraylist 中显示它们,所以我的代码就是这样,我想跳过 -- 行以在 Arraylist 中显示..

private List getQuotes(){私人列表 getQuotes(){

    List<String> quotes = new ArrayList<>();

    BufferedReader bufferedReader = null;

    try {
        InputStream open = getAssets().open("barish.txt");

        bufferedReader = new BufferedReader(new InputStreamReader(open));

        String line;

        while ((line = bufferedReader.readLine()) != null) {




            quotes.add(line);
        }


    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        if (bufferedReader != null) {

            try {
                bufferedReader.close();

            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }
    return quotes;
}

[enter image description here][1] [在此处输入图片说明][1]

txt File image is here [1]: https://i.stack.imgur.com/AiI67.png txt 文件图像在这里 [1]: https : //i.stack.imgur.com/AiI67.png

You could check if the line is not equal to -- before adding it to the quotes list.在将其添加到引号列表之前,您可以检查该行是否不等于 --。

if(line.equals("--") == false) {
     quotes.add(line);
}

Edit: Combining the lines together编辑:将线条组合在一起

In order to combine the strings between the -- lines you could accumulate the quote into a string quoteLine between each --.为了组合 -- 行之间的字符串,您可以将引号累积到每个 -- 之间的字符串quoteLine So if the line is not a -- line then it will be appended to the string quoteLine .因此,如果该行不是 -- 行,那么它将被附加到字符串quoteLine If it is a -- line then it will add the previously constructed quote to the array list and initialize quoteLine to an empty string to reset it.如果它是一个 -- 行,那么它会将先前构造的引号添加到数组列表中,并将quoteLine初始化为空字符串以重置它。

String line;
String quoteLine = "";
while ((line = bufferedReader.readLine()) != null) {

    if(line.equals("--") == false) {
        quotes.add(quoteLine);
        quoteLine = "";
    } else {
        quoteLine += line;
    }
 }

try to use replaceAll method then read the file as above尝试使用replaceAll方法然后读取文件如上

  line = line.replaceAll("--"," ");

//to remove all "--" and replace it with a space //删除所有“--”并将其替换为空格

enter image description here在此处输入图片说明

This is Txt File ..这是 Txt 文件..

This Is A Poetry App.. This 2 Lines Make 1 poetry So I Want To Display every 2 lines together In My Recyclerlist View..这是一个诗歌应用程序.. 这 2 行构成 1 首诗,所以我想在我的回收站视图中显示每 2 行一次..

This The Example...这个例子...

i Want To Display My Poetry Like This In Recycler View我想在回收站视图中像这样显示我的诗歌

enter image description here在此处输入图片说明

You can do something like this:你可以这样做:

public static List<List<String>> getQuotes(String file) {
    List<List<String>> allQuotes = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {

        // Let's have a queue backed by LinkedList to keep the two
        // lines just before the next line that is '--'.
        Queue<String> quotes = new LinkedList<String>();
        String line;

        while ((line = br.readLine()) != null) {

            // If the current line starts with -- and the two previous lines persisted
            // in the quotes queue to allQuotes and clear the quotes. You can do an equals
            // check here if you're certain about no leading and trailing white spaces in
            // each line and it is always --.
            if (line.trim().startsWith("--")) {

                // Don't add quotes to allQuotes when quotes queue is empty.
                // You can also check quotes.size() == 2 along with !quotes.isEmpty()
                // if you want to always have at least two quotes in each sub-list retuned.
                if (!quotes.isEmpty()) {
                    allQuotes.add(new ArrayList(quotes));
                    quotes.clear();
                }
            } else if (!line.trim().isEmpty()) {
                // Add each line into the quotes queue if it is not blank
                // or if it is not --
                quotes.add(line);
            }

            // If the size of queue is > 2 remove an item from from the front,
            // because we are only interested in two lines before --
            if (quotes.size() > 2) {
                quotes.remove();
            }
        }
    } catch (Exception e) {
        e.printStackTrace(); // TODO: Handle exceptions
        return null;
    }
    return allQuotes;
}

I think perhaps you want something more like this:我想也许你想要更像这样的东西:

    public List<List<String>> getQuotes() { 
        List<List<String>> allQuotes = new ArrayList<>();
        BufferedReader bufferedReader = null;
        try {
            InputStream open = getAssets().open("barish.txt");
            bufferedReader = new BufferedReader(new InputStreamReader(open));
            String line;
            ArrayList<String> quote = new ArrayList<>();
            while ((line = bufferedReader.readLine()) != null) {
                if (line.equals("--"))  {
                    if (!quote.isEmpty()) {
                        allQuotes.add(quote);
                    }
                    quote = new ArrayList<>();
                } else {
                    quote.add(line);
                }
            }
            if (!quote.isEmpty()) {
                allQuotes.add(quote);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return allQuotes;
    }

The result is a List<List<String>> : a list of poems, with each poem in its own List<String> .结果是一个List<List<String>> :一个诗歌列表,每首诗歌都在它自己的List<String>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从.txt文件读取随机文本? - How can I read random text from .txt file? 如何在Android的GoogleDrive中读取.txt文件? - How can I read .txt file from GoogleDrive in Android? 如何使用BufferedReader将txt文件中的行读取到数组中 - How do I use BufferedReader to read lines from a txt file into an array 如何从txt文件读取行并将每行添加到LinkedList类型? - How do I read lines from a txt file and add each line to a type LinkedList? 在Java中按下按钮后如何从txt文件读取行 - How to read lines from txt file after button press in Java 如何从输入的文件中读取行,然后将最近读取的行存储在数组中? - How can I read lines from a inputted file and then store the most recently read lines in an array? 如何将.txt文件的最后5行读入Java - How to read last 5 lines of a .txt file into java 如何读取 java 中 txt 文件的某些行? - How to read certain lines of a txt file in java? 我如何使用 trim() 方法来完成这项工作? 我需要读取txt文件,同时忽略空行和以“//”开头的行 - how can i use the trim() method to make this work? I need to read the txt file while ignoring the blank line and lines that start with "//" 从.txt文件中读取名称后,如何打印下3行? - How can I print the next 3 lines after reading a name from a .txt file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM