相对路径与绝对路径构造file对象

 

package file;



import java.io.File;



public class FileTest1 {



	public static void main(String[] args) {

		// TODO Auto-generated method stub

		/*只是建立跟这个文件的联系,不会去查看这个文件是否存在*/

		String parentPath="E:/backup";

		String name="EXPDAT.DMP";

		//使用相对路径去构建file对象

		File src=new File(parentPath,name);

		//file(File parent,String child)  

		//根据parent抽象路径名和child路径名字符串创建一个新的File对象		

		File src1=new File(new File(parentPath),name);

		//file(String parent,String child)  

		//根据parent路径名字符串和child路径名字符串创建一个新的File对象		

		System.out.println(src.getName());

		//file(String parent)

		//绝对路径构建文件

		File src2=new File("E:/backup/2.jpg");

		System.out.println(src2.getPath());

		//没有盘符的情况下

		File src3=new File("2.jpg");

		System.out.println(src3.getPath());

		System.out.println(src3.getAbsolutePath());

		 

	}



}

  

 

运行结果

EXPDAT.DMP

E:\backup\2.jpg

2.jpg

F:\j2ee操作\tt\2.jpg

  

你可能感兴趣的:(File)