开发者

Writing to a File in Java

I'm really new to Java, and I ca开发者_运维知识库n't write to a file for some reason, my code looks like this:

FileWriter fstream;
    try {
        fstream = new FileWriter(fileLocation);
        BufferedWriter out = new BufferedWriter(fstream);
        log.info("test was supposed to be written to the file");
        out.write("test");

        out.flush();
        out.close();

    } catch (IOException e) {
        log.error("File not created ", e);
    }

When I go to the fileLocation, I see my file, but it's empty. My log does say "test was supposed to be written to the file"

What could I be doing wrong here?

Thanks!

UPDATE: My FileLocation variable is a string:

private String fileLocation="/Users/s/out.txt";

I'm using a Mac


Code is fine. Are you checking the right file location? Perhaps you had created the file you're checking before; while your program could be writing elsewhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜