linux和window删除本地文件的代码都是一样的
假设此时在windows本地的D盘根目录下有home文件夹里面有1.jpg图片(D:/home/1.jpg),并且此代码的项目在D盘的某个文件夹下,运行下面程序
File file = new File("/home/1.jpg");
if (file.exists()) {
try {
file.delete();
} catch (Exception e) {
e.printStackTrace();
}
会把D:/home 下的1.jppg图片删掉,虽然new File时没给磁盘路径,但程序会识别项目所在根目录磁盘路径。
证明:
public static void main(String[] args) throws IOException {
File file = new File("/home/1.jpg");
System.out.println(file.getAbsolutePath());
System.out.println(file.exists());
}
而linux下运行代码
new File("/home/1.jpg")
路径就是 根目录下home…