方法名称 | 方法作用 |
---|---|
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:xptest1.txt
1.txt
E:xptest1.txt
test.txt
test.txt
D:codejavaIOtest.txt
.
.
D:codejavaIO.
*/
}