最近遇到一个小东西,File file=new File(“”); 这是什么鬼….于是,我百度和亲自试验了一下.把new File(“.”),new File(“”),new File(“..”),new File(“.\”)都玩了一下,话不多少,先看代码:
package test; import java.io.File; public class PathThings { public static void main(String[] args) { try{ //没有参数 File fileCreateByNo=new File(""); System.out.println("fileCreateByNo=="+fileCreateByNo); System.out.println("fileCreateByNo=="+fileCreateByNo.getCanonicalPath()); System.out.println("-----------------------------"); //一个点的参数 File fileCreateByPoint=new File("."); System.out.println("fileCreateByPoint=="+fileCreateByPoint); System.out.println("fileCreateByPoint=="+fileCreateByPoint.getCanonicalPath()); System.out.println("-----------------------------"); //两个点的参数 File fileTwoPoint = new File(".."); System.out.println("fileTwoPoint=="+fileTwoPoint); System.out.println("fileTwoPoint=="+fileTwoPoint.getCanonicalPath()); System.out.println("-----------------------------"); //一个点两条斜线的参数 File filePLL = new File(".\\"); System.out.println("filePLL=="+filePLL); System.out.println("filePLL=="+filePLL.getCanonicalPath()); System.out.println("-----------------------------"); //当前工作目录 String currentWorkPath=System.getProperty("user.dir"); System.out.println("currentWorkPath=="+currentWorkPath); }catch(Exception e){ System.out.println("IOException....出问题咯"); } } }然后一运行,效果是这样的: 是不是一看就懂了.就是当前项目工作目录. 这一般就是在运行的时候,需要在当前工作目录进行一些拷贝等操作,将项目中的东西拷贝出去等….自由发挥咯. 这个demo很简单,但是蕴含着大道理. 欢迎大家指点,评论和点赞. 加油吧,年轻的程序员.