英文:
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 "**javac appdb/CreateAndWrite.java**" so the file was created there thus I couldn'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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论