1

I am reading the contents of a URL and write a file the problem is that I'm not able to write all the content in the file and do not know what I'm doing wrong.

My code,

try {
            URL url = new URL(sourceUri);
            URLConnection conn = url.openConnection();

            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));


            file.getParentFile().mkdirs();
            file.createNewFile();

            FileWriter fw = new FileWriter(file);
            BufferedWriter bw  = new BufferedWriter(fw);

            while ((inputLine = br.readLine()) != null) {
                bw.write(inputLine + System.getProperty("line.separator"));
            }

            br.close();

            System.out.println("DONE");

        }catch (IOException ioe){
            ioe.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }

        return ontologies;
    }

Please help

aedsferrao
  • 33
  • 2
  • 6
  • 1
    First things first: use java.nio.file. Second: are you absolutely sure that this is a text file? Why don't you just copy the `InputStream`? – fge May 22 '15 at 10:46
  • @aioobe not duplicate; this answer uses an obsolete API when in 2015 you have java.nio.file, see my answer. – fge May 22 '15 at 10:50
  • @fge, I don't see anything in the linked question that mentions obsolete API. (Some answers are using an obsolete API, sure, but the correct procedure here would be to post an answer to *that* question and close this one as a dup I think.) – aioobe May 22 '15 at 10:51
  • @aioobe hint: `File` – fge May 22 '15 at 10:52
  • @fge, I still don't see any `File` in the question. Just because the answers are obsolete doesn't mean a new question should be posted. – aioobe May 22 '15 at 10:52
  • @aioobe hint: `FileOutputStream` – fge May 22 '15 at 10:53
  • Are you reading my comments? – aioobe May 22 '15 at 10:53
  • @aioobe I do; and what do you think `file.getParentFile().mkdirs()` do? – fge May 22 '15 at 11:00
  • aioobe missing this two lines bw.flush(); bw.close(); Thanks for all the help – aedsferrao May 22 '15 at 11:04
  • @aedsferrao you should really consider using java.nio.file. Using `File` in 2015 is an anachronism. Look at how simple it is with my answer. – fge May 22 '15 at 11:12

2 Answers2

6

You are doing many things incorrectly.

First: you don't close all your resources; where is the writer to the file closed?

Second: you use new InputStreamReader(...) without specifying the encoding. What says that the encoding on the other end is the one of your JVM/OS combination?

Last but not least, and in fact, this is the most important, you should use java.nio.file. This is 2015 after all.

Simple solution:

final Path path = file.toPath(); // or rather use Path directly
Files.createDirectories(path.getParent());

try (
    final InputStream in = conn.getInputStream();
) {
    Files.copy(in, path);
}

Done, encoding independent, and all resources closed.

fge
  • 119,121
  • 33
  • 254
  • 329
  • looks like there are issues with Files.copy https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java#comment63880638_24041297 – The Student Mar 27 '18 at 16:35
1

The problem is you're using a BufferedWriter and you don't close it. It has some content in his buffer that is not writing and you're missing.

Try flushing the buffer and closing the BufferedWriter:

bw.flush();
bw.close();

Include this two lines after before your br.close();.

Also you can read how BufferedWriter works here.

And I think you should close FileWriter, too, in order to unblock the file.

fw.close();

EDIT 1:

Closing the BufferedWriter will flush the buffer for you. You need only to close it.

Jan Nielsen
  • 10,892
  • 14
  • 65
  • 119
piraces
  • 1,320
  • 2
  • 18
  • 42