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

10
  • 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? Commented May 22, 2015 at 10:46
  • @aioobe not duplicate; this answer uses an obsolete API when in 2015 you have java.nio.file, see my answer. Commented May 22, 2015 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.) Commented May 22, 2015 at 10:51
  • @aioobe hint: File Commented May 22, 2015 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. Commented May 22, 2015 at 10:52

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

looks like there are issues with Files.copy stackoverflow.com/questions/921262/…
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.

1 Comment

you're right, edited.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.