簡體   English   中英

如何從txt文件中讀取2行

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

嘿伙計們,我正在處理這個問題,我想從 txt 文件合並到行並在 Arraylist 中顯示它們,所以我的代碼就是這樣,我想跳過 -- 行以在 Arraylist 中顯示..

私人列表 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;
}

[在此處輸入圖片說明][1]

txt 文件圖像在這里 [1]: https : //i.stack.imgur.com/AiI67.png

在將其添加到引號列表之前,您可以檢查該行是否不等於 --。

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

編輯:將線條組合在一起

為了組合 -- 行之間的字符串,您可以將引號累積到每個 -- 之間的字符串quoteLine 因此,如果該行不是 -- 行,那么它將被附加到字符串quoteLine 如果它是一個 -- 行,那么它會將先前構造的引號添加到數組列表中,並將quoteLine初始化為空字符串以重置它。

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

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

嘗試使用replaceAll方法然后讀取文件如上

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

//刪除所有“--”並將其替換為空格

在此處輸入圖片說明

這是 Txt 文件..

這是一個詩歌應用程序.. 這 2 行構成 1 首詩,所以我想在我的回收站視圖中顯示每 2 行一次..

這個例子...

我想在回收站視圖中像這樣顯示我的詩歌

在此處輸入圖片說明

你可以這樣做:

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;
}

我想也許你想要更像這樣的東西:

    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;
    }

結果是一個List<List<String>> :一個詩歌列表,每首詩歌都在它自己的List<String>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM