Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    /**
     * Parses the M3U file and returns the first file name.
     * 
     * @param inputUrl
     *            The input M3U file.
     * @return The first file name in the M3U play list.
     */
    public static String parseM3U(String inputUrl) {
        String inputFile = "";
        try {
            String plsContents = readTextFromUrl(new URL(inputUrl));
            for (String line : plsContents.split("\n")) {
                if (!line.trim().isEmpty() && !line.trim().startsWith("#")) {
                    inputFile = line.trim();
                    break;
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return inputFile;
    }

    /**
     * Return the text of the file with the given URL. E.g. if
     * http://test.be/text.txt is given the contents of text.txt is returned.
     * 
     * @param url
     *            The URL.
     * @return The contents of the file.
     */
    public static String readTextFromUrl(URL url) {
        StringBuffer fubber = new StringBuffer();
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                fubber.append(inputLine).append("\n");
            }
            in.close();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
        return fubber.toString();
    }
}