简体   繁体   English

使用Java编写文件不会在Mac OSx中保存文件

[英]Writing a file using Java does not save the file in Mac OSx

I am developing a java application in which I need to save some data in a file. 我正在开发一个Java应用程序,其中需要将一些数据保存在文件中。 I tried to use different methods, however no file gets saved and I don't receive any errors. 我尝试使用其他方法,但是没有保存任何文件,并且没有收到任何错误。 Here is my code for saving the file: 这是我保存文件的代码:

String filename = "/Users/name.txt";
    FileWriter fstream;

    try {
        fstream = new FileWriter(filename);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write("My Name is Bobby Bob");
        out.newLine();                                  
        out.flush();
        out.close();
    } 
    catch (IOException e) {
        e.printStackTrace();          
    }

I tried even making the file myself but it remains empty. 我什至尝试自己制作文件,但该文件仍然为空。

It's unlikely that you can write directly to the Users folder through Java on Mac OSX. 在Mac OSX上,您不太可能通过Java直接写入Users文件夹。

Try this to make sure: 尝试执行以下操作以确保:

String filename = "/Users/name.txt";
FileWriter fstream;
try {
    File file = new File(filename);
    // checking if we can write this file
    System.out.println("Can write? " + file.canWrite());
    fstream = new FileWriter(file);
    BufferedWriter out = new BufferedWriter(fstream);
    out.write("My Name is Bobby Bob");
    out.newLine();
    out.flush();
    out.close();
} catch (IOException e) {
    e.printStackTrace();
}

Output expected: 预期输出:

Can write? false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM