new File("E:") 文件路径变成项目所在的文件夹
项目路径为:E:\TEST2; 测试代码如下:
public static void main(String[] args) { File file1 = new File("D:"); System.out.println(file1.getAbsolutePath());//D:\ File file2 = new File("D:\\"); System.out.println(file2.getAbsolutePath());//D:\ File file3 = new File("E:"); System.out.println(file3.getAbsolutePath());//E:\TEST2 File file4 = new File("E:\\"); System.out.println(file4.getAbsolutePath());//E:\ File file5 = new File("E:\\aaaa"); System.out.println(file5.getAbsolutePath());//E:\aaaa File file6 = new File("E:\\aaaa\\aaaa.txt"); System.out.println(file6.getAbsolutePath());//E:\aaaa\aaaa.txt }
只有file3的路径变为项目所在的路径,其他file的路径都正常;
项目文件所在盘符和要获取的盘符一致时用 new File("E:\\") 的方式