java - How to put "BufferedReader Line into ArrayList"
问题描述
I am setting up a class (see below) that creates a playlist based on a string that was returned from a urlConnectReader.
I am attempting to use a BufferedReader to read each line of the string which works fine by itself.
I get an error when I try to add the current line from the BufferedReader to an ArrayList.
My project keeps returning null but when I remove the ArrayList from the function below it shows each line from the string how I want it in my info.logger but I need this to appear in an ArrayList.
public class Playlist {
String output = "'";
ArrayList<String> contents;
//String[] content = new String[10];
public Playlist(String output) {
// Sets url data to the content string
this.output = output;
}
//Converts String to ArrayList
public ArrayList<String> addContentToArray(String output) throws IOException {
String line = "";
ByteArrayInputStream stream = new ByteArrayInputStream(output.getBytes(StandardCharsets.UTF_8));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
try {
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
contents.add(line);
}} catch (Exception e) {
System.out.println("Uh Oh, array is empty");
}
return contents;
}
public void print() {
System.out.println(contents);
}
}
I am trying to covert each line of the following String into an ArrayList:
EXTM3U
EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=232370,CODECS="mp4a.40.2,avc1.4d4015" gear1/prog_index.m3u8
EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=649879,CODECS="mp4a.40.2,avc1.4d401e" gear2/prog_index.m3u8
EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=991714,CODECS="mp4a.40.2,avc1.4d401e" gear3/prog_index.m3u8
EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1927833,CODECS="mp4a.40.2,avc1.4d401f" gear4/prog_index.m3u8"
Without the ArrayList in the function it will work ???
解决方案
推荐阅读
- python - 如何在 pinterest 中抓取日期
- node.js - 有什么方法可以从 Node.js 替换整个 MongoDB 中的集合中的特定字符串
- postgresql - 逻辑复制槽的restart_lsn位置移动很慢
- python - 获取主键组合对应的最大值
- javascript - XPATH 做一个没有双胞胎的元素列表
- android - Android中的OkHttp Mutual SSL
- mysql - Docker Compose mysql 环境变量与 Application .env 数据库变量。很困惑
- ti-basic - TI-82 字符串变量?
- python - Pandas:使用列在两个 DF 上左合并,但只取另一列的最后一个(或平均值)
- audio - 如何找到在一定时间内使用的静默字节数?