Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java createNewFile()之后的myfile在哪里_Java_Createfile - Fatal编程技术网

Java createNewFile()之后的myfile在哪里

Java createNewFile()之后的myfile在哪里,java,createfile,Java,Createfile,可能重复: 我正在尝试在当前目录中创建一个txt文件。我运行了下面的代码,没有错误。但是我在任何地方都找不到myfile.txt。我想在当前目录中创建myfile.txt。我该怎么做 我使用的代码是: public void createFile(){ try{ File f = new File("myfile.txt"); if (!f.exists()){ f.createNewFile();

可能重复:

我正在尝试在当前目录中创建一个txt文件。我运行了下面的代码,没有错误。但是我在任何地方都找不到myfile.txt。我想在当前目录中创建myfile.txt。我该怎么做

我使用的代码是:

public void createFile(){
        try{
            File f = new File("myfile.txt");
            if (!f.exists()){
                f.createNewFile();
                System.out.println("New file \"myfile.txt\" has been created");
            }
        }catch(Exception e){
            System.out.println("Error while creating file " + e);
        }   
    }

它位于用户目录中,可通过以下方式检索:

 System.getProperty("user.dir");
详情请参见此处:


尝试在控制台上打印:

System.out.println(f.getAbsolutePath());

这肯定会告诉你的。

它就是这么做的。我建议您打印您的cwd
System.getProperty(“user.dir”)
。如果你想把一些东西放进文件,你不需要这样做。