gpt4 book ai didi

java - 文件不是用 Java 创建的 (Eclipse)

转载 作者:行者123 更新时间:2023-11-29 09:43:48 24 4
gpt4 key购买 nike

所以我一直在做最简单的事情。为 Java 应用程序创建一个文本文件。直接在C目录下即可:

File file = new File("C://function.txt");
System.out.println(file.exists());

虽然文件从未出现,但我更改了斜线,更改了路径,什么也没有。有人可以帮我吗?

最佳答案

java新建文件的方法有很多:(首先要验证在c:文件夹下新建文件的权限)

  1.  String path = "C:"+File.separator"function.txt";
    File f = new File(path);
    f.mkdirs();
    f.createNewFile();

    __ 或

     try {
    //What ever the file path is.
    File f = new File("C:/function.txt");
    FileOutputStream is = new FileOutputStream(f);
    OutputStreamWriter osw = new OutputStreamWriter(is);
    Writer w = new BufferedWriter(osw);
    w.write("Line 1!!");
    w.close();
    } catch (IOException e) {
    System.err.println("Problem writing to the file function.txt");
    }

关于java - 文件不是用 Java 创建的 (Eclipse),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22279730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com