IDEA相对路径问题
程序员文章站
2022-05-10 11:49:53
...
IDEA相对路径问题
public class MainSystem {
public static void main(String[] args) throws IOException {
ArrayList<String> array= new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
BufferedWriter bw=new BufferedWriter(new FileWriter("test2/xx.txt"));
for(String s:array){
bw.write(s);
bw.newLine();
bw.flush();
}
bw.close();
}
}
如以上所示,在写相对路径时可以直接写当前文件所在的包名加要创建的文件名。
如果写成
BufferedWriter bw=new BufferedWriter(new FileWriter("HelloWorld/test2/xx.txt"));
则会报错
Exception in thread "main" java.io.FileNotFoundException: HelloWorld\test2\xx.txt (系统找不到指定的路径。)
at java.base/java.io.FileOutputStream.open0(Native Method)
at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:126)
at java.base/java.io.FileWriter.<init>(FileWriter.java:66)
at com.lwh.test.MainSystem.main(MainSystem.java:12)