用Java编写文件 [英] Writing to a File in Java

查看:102
本文介绍了用Java编写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java真的很陌生,因为某些原因,我无法写入文件,我的代码如下所示:

  FileWriter fstream; 
尝试{
fstream = new FileWriter(fileLocation);
BufferedWriter out = new BufferedWriter(fstream);
log.info(测试应该被写入文件);
out.write(test);

out.flush();
out.close();
$ b $ catch(IOException e){
log.error(File not created,e);
}

当我进入fileLocation时,我看到我的文件,但是它是空的。我的日志确实说测试应该被写入文件

我在这里做错了什么?

谢谢!

更新:我的FileLocation变量是一个字符串:

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

我正在使用Mac

解决方案

代码很好。你正在检查正确的文件位置?也许你已经创建了你之前检查的文件;而你的程序可以在其他地方写。


I'm really new to Java, and I can'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.

这篇关于用Java编写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆