如何从txt文件中读取两行内容

huangapple go评论75阅读模式
英文:

How Can I Read 2 lines From txt File

问题

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) {
        if (!line.startsWith("--")) { // Skip lines starting with "--"
            quotes.add(line);
        }
    }

} catch (IOException e) {

    e.printStackTrace();

} finally {

    if (bufferedReader != null) {

        try {
            bufferedReader.close();

        } catch (IOException e) {

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

}

英文:

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<String> getQuotes(){

    List&lt;String&gt; quotes = new ArrayList&lt;&gt;();

    BufferedReader bufferedReader = null;

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

        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

txt File image is here
1: https://i.stack.imgur.com/AiI67.png

答案1

得分: 1

你可以在将其添加到引用列表之前,检查该行是否不等于 --。

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;
    }
}
英文:

You could check if the line is not equal to -- before adding it to the quotes list.

if(line.equals(&quot;--&quot;) == 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 = &quot;&quot;;
while ((line = bufferedReader.readLine()) != null) {

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

答案2

得分: 1

尝试使用 replaceAll 方法,然后按照上述方式读取文件。

line = line.replaceAll("--", " ");
//将所有的 "--" 替换为空格
英文:

try to use replaceAll method then read the file as above

  line = line.replaceAll(&quot;--&quot;,&quot; &quot;);

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

答案3

得分: 1

这是一个文本文件..

这是一个诗歌应用.. 这两行组成一首诗
因此,我想在我的Recyclerlist View中一起显示每两行..

这是示例...

我想在Recycler View中像这样显示我的诗歌:

英文:

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

答案4

得分: 0

你可以像这样做:

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

        // 让我们使用由 LinkedList 支持的队列,以保留下一个行为 '--' 的前两行。
        Queue<String> quotes = new LinkedList<>();
        String line;

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

            // 如果当前行以 -- 开头,并且前两行在引号队列中持久化,
            // 则将其添加到 allQuotes,并清空引号。如果你确定每行中没有前导和尾随空格,
            // 并且它总是以 -- 开头,你可以在此处进行相等性检查。
            if (line.trim().startsWith("--")) {

                // 当引号队列为空时,不要将引号添加到 allQuotes。
                // 如果你希望每个子列表中始终至少有两个引号,
                // 你还可以检查 quotes.size() == 2 以及 !quotes.isEmpty()。
                if (!quotes.isEmpty()) {
                    allQuotes.add(new ArrayList(quotes));
                    quotes.clear();
                }
            } else if (!line.trim().isEmpty()) {
                // 如果行不为空白或不是 --,则将每行添加到引号队列中。
                quotes.add(line);
            }

            // 如果队列的大小 > 2,则从前面移除一个项,
            // 因为我们只关注 -- 前的两行。
            if (quotes.size() > 2) {
                quotes.remove();
            }
        }
    } catch (Exception e) {
        e.printStackTrace(); // TODO: 处理异常
        return null;
    }
    return allQuotes;
}
英文:

You can do something like this:

public static List&lt;List&lt;String&gt;&gt; getQuotes(String file) {
List&lt;List&lt;String&gt;&gt; allQuotes = new ArrayList&lt;&gt;();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
// Let&#39;s have a queue backed by LinkedList to keep the two
// lines just before the next line that is &#39;--&#39;.
Queue&lt;String&gt; quotes = new LinkedList&lt;String&gt;();
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&#39;re certain about no leading and trailing white spaces in
// each line and it is always --.
if (line.trim().startsWith(&quot;--&quot;)) {
// Don&#39;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 &gt; 2 remove an item from from the front,
// because we are only interested in two lines before --
if (quotes.size() &gt; 2) {
quotes.remove();
}
}
} catch (Exception e) {
e.printStackTrace(); // TODO: Handle exceptions
return null;
}
return allQuotes;
}

答案5

得分: 0

我认为也许你想要类似这样的代码:

    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> 中。

英文:

I think perhaps you want something more like this:

    public List&lt;List&lt;String&gt;&gt; getQuotes() { 
List&lt;List&lt;String&gt;&gt; allQuotes = new ArrayList&lt;&gt;();
BufferedReader bufferedReader = null;
try {
InputStream open = getAssets().open(&quot;barish.txt&quot;);
bufferedReader = new BufferedReader(new InputStreamReader(open));
String line;
ArrayList&lt;String&gt; quote = new ArrayList&lt;&gt;();
while ((line = bufferedReader.readLine()) != null) {
if (line.equals(&quot;--&quot;))  {
if (!quote.isEmpty()) {
allQuotes.add(quote);
}
quote = new ArrayList&lt;&gt;();
} 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&lt;List&lt;String&gt;&gt;: a list of poems, with each poem in its own List&lt;String&gt;.

huangapple
  • 本文由 发表于 2020年8月29日 11:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63643022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定