Java-如何将文件写入指定目录

发布于 2024-11-03 08:58:59 字数 590 浏览 4 评论 0原文

我想将文件 results.txt 写入我的计算机上的特定目录(准确地说是 Z:\results)。如何指定 BufferedWriter/FileWriter 的目录?

目前,它成功写入文件,但写入我的源代码所在的目录。谢谢

    public void writefile(){

    try{
        Writer output = null;
        File file = new File("results.txt");
        output = new BufferedWriter(new FileWriter(file));

        for(int i=0; i<100; i++){
           //CODE TO FETCH RESULTS AND WRITE FILE
        }

        output.close();
        System.out.println("File has been written");

    }catch(Exception e){
        System.out.println("Could not create file");
    }
}

I want to write a file results.txt to a specific directory on my machine (Z:\results to be precise). How do I go about specifying the directory to BufferedWriter/FileWriter?

Currently, it writes the file successfully but to the directory where my source code is located. Thanks

    public void writefile(){

    try{
        Writer output = null;
        File file = new File("results.txt");
        output = new BufferedWriter(new FileWriter(file));

        for(int i=0; i<100; i++){
           //CODE TO FETCH RESULTS AND WRITE FILE
        }

        output.close();
        System.out.println("File has been written");

    }catch(Exception e){
        System.out.println("Could not create file");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抚笙 2024-11-10 08:58:59

您应该使用 File 的辅助构造函数,用于指定要在其中以符号方式创建的目录。这很重要,因为通过将目录名添加到原始名称来创建文件的答案并不像此方法那样独立于系统。

示例代码:

String dirName = /* something to pull specified dir from input */;

String fileName = "test.txt";
File dir = new File (dirName);
File actualFile = new File (dir, fileName);

/* rest is the same */

希望有帮助。

You should use the secondary constructor for File to specify the directory in which it is to be symbolically created. This is important because the answers that say to create a file by prepending the directory name to original name, are not as system independent as this method.

Sample code:

String dirName = /* something to pull specified dir from input */;

String fileName = "test.txt";
File dir = new File (dirName);
File actualFile = new File (dir, fileName);

/* rest is the same */

Hope it helps.

韶华倾负 2024-11-10 08:58:59

使用:

File file = new File("Z:\\results\\results.txt");

Windows中,您需要将反斜杠加倍,因为反斜杠字符本身是Java文字字符串中的转义符。

对于Linux等POSIX系统,只需使用默认文件路径即可,无需加倍正斜杠。这是因为正斜杠在 Java 中不是转义字符。

File file = new File("/home/userName/Documents/results.txt");

Use:

File file = new File("Z:\\results\\results.txt");

You need to double the backslashes in Windows because the backslash character itself is an escape in Java literal strings.

For POSIX system such as Linux, just use the default file path without doubling the forward slash. this is because forward slash is not a escape character in Java.

File file = new File("/home/userName/Documents/results.txt");
无力看清 2024-11-10 08:58:59

只需将完整的目录位置放入 File 对象中即可。

File file = new File("z:\\results.txt");

Just put the full directory location in the File object.

File file = new File("z:\\results.txt");
疯到世界奔溃 2024-11-10 08:58:59

最佳实践是在路径中使用 File.separator。

The best practice is using File.separator in the paths.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文