相对绝对路径
* 路径分隔符 ;
* 文件分隔符 \
package mypro04; /** * 路径分隔符 ; * 文件分隔符 \ */ import java.io.File; public class TestFIile { public static void main(String[] args) { //路径分隔符 ; System.out.println(File.pathSeparator); //文件分隔符 \ System.out.println(File.separator); System.out.println("***********相对路径*******************"); //相对路径 String parentpath="D:/learn/java/mycode"; //"D:/learn/java/mycode/" String name="Welcome.class"; File file=new File(parentpath,name); System.out.println(file.getName()); //Welcome.class System.out.println(file.getPath()); //D:\learn\java\mycode\Welcome.class System.out.println(file.getParent()); //D:\learn\java\mycode System.out.println(file.getAbsolutePath());//D:\learn\java\mycode\Welcome.class System.out.println("**********绝对路径********************"); //绝对路径 String absolutePath="D:/learn/java/mycode/Welcome.class"; File file1=new File(absolutePath); System.out.println(file1.getName()); // Welcome.class System.out.println(file1.getPath());// D:\learn\java\mycode\Welcome.class System.out.println(file1.getParent());// D:\learn\java\mycode System.out.println(file1.getAbsolutePath());// D:\learn\java\mycode\Welcome.class System.out.println("**********没有盘符********************"); //没有盘符 ;,以user.dir构建 File file2=new File("Welcome.class"); System.out.println(file2.getName()); // Welcome.class System.out.println(file2.getPath());// Welcome.class System.out.println(file2.getParent());// null System.out.println(file2.getAbsolutePath());// D:\software\eclipse\eclipse-workspace\mypro04\Welcome.class } }
posted on 2020-03-15 16:14 happygril3 阅读(138) 评论(0) 收藏 举报
浙公网安备 33010602011771号