Ank*_*kur 3 java file relative-path
我正在尝试使用创建文件
文件newFile =新文件("myFile");
但是,没有创建名为"myFile"的文件.这是在一个Web应用程序项目中,即正确的形式作为WAR包装,但我将其称为主方法的一部分(只是为了看看它是如何工作的).
如何创建它以便在相对于当前位置的位置创建新文件,即不必放入绝对路径.
编辑:
newFile.createFile();
似乎不起作用:
这是整个代码:
import java.io.File;
import java.io.IOException;
public class Tester {
public static void main(String[] args) throws IOException{
Tester test = new Tester();
test.makeFile();
}
public void makeFile() throws IOException{
File newFile = new File("myFile");
newFile.createNewFile();
}
}
Run Code Online (Sandbox Code Playgroud)
在回答你的评论.除非您另行指定,否则将在进程的当前目录中创建该文件.
// new file in current directory
File f = new File("yourFile");
f.createNewFile();
System.out.println("Path:" + f.getAbsolutePath());
Run Code Online (Sandbox Code Playgroud)
要在您选择的目录中创建它:
File f = new File("c:\\yourDirectory","yourFile");
f.createNewFile();
System.out.println("Path:" + f.getAbsolutePath());
Run Code Online (Sandbox Code Playgroud)