如何将“ BufferedReader行放入ArrayList”

时间:2019-04-05 15:44:30

标签: java string arraylist inputstream bufferedreader

我正在设置一个类(请参见下文),该类基于从urlConnectReader返回的字符串创建播放列表。

我正在尝试使用BufferedReader读取字符串的每一行,这些行本身可以正常工作。

当我尝试将当前行从BufferedReader添加到ArrayList时出现错误。

我的项目一直返回null,但是当我从下面的函数中删除ArrayList时,它在字符串中显示了我希望在info.logger中显示的每一行,但是我需要将其显示在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);
    }

}

我正在尝试将以下字符串的每一行隐藏到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“

在该函数中没有ArrayList的情况下将起作用???

0 个答案:

没有答案