簡體   English   中英

用Java寫入文件

[英]Writing to a File in Java

我是Java新手,我出於某種原因無法寫入文件,我的代碼如下所示:

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);
    }

當我進入fileLocation時,我看到了我的文件,但它是空的。 我的日志確實說“測試應該寫入文件”

我在這里做錯了什么?

謝謝!

更新:我的FileLocation變量是一個字符串:

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

我正在使用Mac

代碼很好。 你在檢查正確的文件位置嗎? 也許你之前創建過你正在檢查的文件; 而你的程序可能寫在別處。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM