简体   繁体   中英

How To Read Second Line In Text With Java

I am trying to create a simple command line program that will determine if a playlist is a media playlist or master based on the tag returned. Unfortunately both type of playlist first line tags are the same so I was wondering is their a way I could adjust my code to read the text starting at the second line?

 private static String getPlaylistUrl(String theUrl) throws FileNotFoundException, MalformedURLException, IOException{ String content = ""; //Creates a url variable URL url = new URL(theUrl); //Cretes a urlConnection variable URLConnection urlConnection = (HttpURLConnection) url.openConnection(); //Wraps the urlConnection in a BufferedReader BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String line; while ((line = bufferedReader.readLine()) != null) { content += line + "\\n"; } bufferedReader.close(); return content; } 

只需在循环开始前阅读第一行。

private static String getPlaylistUrl(String theUrl) throws IOException {
    try (InputStream is = new URL(theUrl).openConnection().getInputStream();
         BufferedReader reader = new BufferedReader(new InputStreamReader(is));
         Stream<String> stream = reader.lines()) {
        return stream
                // skip the first line
                .skip(1)
                // join all other lines using a new line delimiter
                .collect(Collectors.joining("\n"));
    }
}

Skip the header like this

String line;
bool IsHeader=true;

        while ((line = bufferedReader.readLine()) != null) {
        if (IsHeader){
            IsHeader=false; //skip header..

        }else{
        content += line + "\n";
        }

        }
        bufferedReader.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM