简体   繁体   中英

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..

private List 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]

txt File image is here [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 --. So if the line is not a -- line then it will be appended to the string 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.

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

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

//to remove all "--" and replace it with a space

enter image description here

This is Txt File ..

This Is A Poetry App.. This 2 Lines Make 1 poetry So I Want To Display every 2 lines together In My Recyclerlist View..

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> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM