本地eclipse用相对路径创建文件创建在哪个盘符下:在eclipse的项目代码所在的盘符,而不是eclipse的安装位置所在的盘符。

例:

String f2 = "/test/ttt/ttttt.txt";
			String path2  = f2.substring(0,f2.lastIndexOf("/"));
			System.out.println(path2);
			File direc2 = new File(path2);
			if(!direc2.exists()){
				direc2.mkdirs();
			}
			File tr2 = new File(f2);
			tr2.createNewFile();
			
			System.out.println(new File(f2).getAbsolutePath());//D:\test\ttt\ttttt.txt
			String f3 = "/test/ttt/test1.txt";
			System.out.println(new File(f3).exists());//true



我的eclipse安装在E盘,E:\eclipse

代码在D盘,D:\testEclipse

以上代码则生成在D盘,文件目录:D:\test\ttt\ttttt.txt

读取文件同理,用相对路径读取文件也是从eclipse中代码所在的盘符下读取

你试试吧。


本文转载:CSDN博客