Loading

构建File对象

方法名称 方法作用
getName() 文件名称
getPath() 赋值路径(绝对\相对)
getAbsolutePath() 绝对路径
package cn.hxh.io.file;

import java.io.*;

public class Demo02 {

	public static void main(String[] args) {
		String parentPath = "E:/xp/test";
		String name = "1.txt";
		
		//相对路径
		File src = new File(parentPath, name);
		System.out.println(src.getName());
		src = new File(new File(parentPath), name);
		System.out.println(src.getPath());
		
		//绝对路径
		src = new File("E:/xp/test/1.txt");
		System.out.println(src.getName());
		System.out.println(src.getPath());
		//没有盘符:以user.dir构建
		src = new File("test.txt");
		System.out.println(src.getName());
		System.out.println(src.getPath());//打印赋值路径(绝对/相对)
		System.out.println(src.getAbsolutePath());//打印绝对路径
		
		src = new File(".");
		System.out.println(src.getName());
		System.out.println(src.getPath());//打印赋值路径(绝对/相对)
		System.out.println(src.getAbsolutePath());//打印绝对路径
	}
/*
1.txt
E:\xp\test\1.txt
1.txt
E:\xp\test\1.txt
test.txt
test.txt
D:\code\java\IO\test.txt
.
.
D:\code\java\IO\.
*/
}

posted @ 2020-01-25 22:04  shimeath  阅读(163)  评论(0)    收藏  举报