Java没有创建文件。

huangapple go评论57阅读模式
英文:

Java not creating the file

问题

package appdb;

import java.io.*;

public class CreateAndWrite {

    public static void main(String[] args) throws Exception {
        try {
            File myFile = new File("myFile.txt");
            FileWriter writer = new FileWriter(myFile, true);

            writer.write("在文件中写入这些内容");
            writer.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
英文:

I have a program with the package name "appdb" then i want to create the .txt file, it is compiling and running fine thus showing me that the file was created since i can't see it in the working directory where i expected it to be.

package appdb;

import java.io.*;

public class CreateAndWrite{

    public static void main(String[] args) throws Exception {
        try {
            File myFile= new File("myFile.txt");
            FileWriter writer = new FileWriter(myFile,true);

            writer.write("Write this in the file");
            writer.close();
        } catch(IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

</details>


# 答案1
**得分**: 1

谢谢你的帮助我已经得到了答案问题是我之前是从父文件夹运行和编译该文件:“**javac appdb/CreateAndWrite.java**”,所以文件被创建在那里因此我在工作目录中看不到它但是我只是把要创建的文件的路径从之前的**myFile.txt**改成了**appdb/myFile.txt**然后一切都按照我预期的方式运行了

<details>
<summary>英文:</summary>

Thanks for your help, I got the answer. The problem was that I was running and compiling the file from the parent folder &quot;**javac appdb/CreateAndWrite.java**&quot; so the file was created there thus I couldn&#39;t see it in the working directory. But I only changed the path of the file to be created ,before:**myFile.txt**,after: **appdb/myFile.txt** and things worked as I expected.

</details>



huangapple
  • 本文由 发表于 2020年4月9日 16:58:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/61117445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定