java – How to copy mp3 file from internet?

Question:

The question is very simple. My code copies, but there are some interruptions during playback. What have I done wrong?

        try {
        URL url = new URL("http://cdn.echo.msk.ru/snd/2017-07-28-osoboe-1908.mp3");
        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:/ww.mp3")));
        int c=0;
        while ((c=br.read())!=-1)
            bw.write(c);
        br.close();
        bw.close();
    } catch (MalformedURLException e){}
    catch (IOException e){}
    System.out.println("End");

Answer:

this way it is easier to do:

URL website = new URL("http://cdn.echo.msk.ru/snd/2017-07-28-osoboe-1908.mp3");
Path target = Paths.get("file.mp3");
try (InputStream in = website.openStream()) {
    Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
}
Scroll to Top