首页 > 其他 > 详细

相对绝对路径

时间:2020-03-15 16:25:34      阅读:53      评论:0      收藏:0      [点我收藏+]

* 路径分隔符 ;
* 文件分隔符 \
* getPath():返回 【抽象路径名】--【字符串】
* getAbsolutePath():
* --【抽象路径名】是【绝对路径名】,getPath() 方法一样
* --【抽象路径名】是【空抽象路径名】, 返回【user.dir指定的路径】--字符串

package mypro04;
/**
 * 路径分隔符  ;
 * 文件分隔符  * getPath():返回 【抽象路径名】--【字符串】
 * getAbsolutePath():
 *     --【抽象路径名】是【绝对路径名】,getPath() 方法一样
 *     --【抽象路径名】是【空抽象路径名】, 返回【user.dir指定的路径】--字符串
 */

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
                
    }


}

 

相对绝对路径

原文:https://www.cnblogs.com/hapyygril/p/12498158.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!