首页 > 解决方案 > 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 ???

标签: javastringarraylistinputstreambufferedreader

解决方案


推荐阅读