码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-22 23:04:34      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

 

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对象

标签:

原文地址:http://www.cnblogs.com/tianhao/p/4242902.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!