问题描述
嘿,我正在处理此问题 我想合并到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首诗歌 所以我想在“ Recyclerlist”视图中同时显示每两行。
这个例子...
我想在回收者视图中显示我的诗歌
,您可以执行以下操作:
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>
中。