java 读取本地文件路径_关于Java读取本地文件路径的探究

Eclipse的工作空间路径是:D:\AJavaEE\EclipseWorkSpace,做这个测试的工程名叫PathDemo,jdk版本为1.8。工程的目录层级图如下所示:

520b50f5c7dbb5bc4e72384f31351196.png

一、利用创建File实例读取文件

例1.1 参数传”/”

File fileRoot1 = new File(“/”);

System.out.println(“绝对路径:” + fileRoot1.getAbsolutePath());

System.out.println(“标注路径:” + fileRoot1.getCanonicalPath());

System.out.println(“相对路径:” + fileRoot1.getPath());

控制台打印如下:

绝对路径:D:\

标注路径:D:\

相对路径:\

例1.2 参数传”“(空串)

File fileRoot2 = new File(“”);

System.out.println(“绝对路径:” + fileRoot2.getAbsolutePath());

System.out.println(“标注路径:” + fileRoot2.getCanonicalPath());

System.out.println(“相对路径:” + fileRoot2.getPath());

控制台打印如下:

绝对路径:D:\AJavaEE\EclipseWorkSpace\PathDemo

标注路径:D:\AJavaEE\EclipseWorkSpace\PathDemo

相对路径:

例1.3 参数传”a.txt”

File fileA = new File(“a.txt”);

System.out.println(“绝对路径:” + fileA.getAbsolutePath() + “,文件是否存在:” + fileA.exists());

System.out.println(“标注路径:” + fileA.getCanonicalPath() + “,文件是否存在:” + fileA.exists());

System.out.println(“相对路径:” + fileA.getPath() + “,文件是否存在:” + fileA.exists());

控制台打印如下:

绝对路径:D:\AJavaEE\EclipseWorkSpace\PathDemo\a.txt,文件是否存在:false

标注路径:D:\AJavaEE\EclipseWorkSpace\PathDemo\a.txt,文件是否存在:false

相对路径:a.txt,文件是否存在:false

例1.4 参数传”src/com/study/path/a.txt”

File fileA1 = new File(“src/com/study/path/a.txt”);

System.out.println(“绝对路径:” + fileA1.getAbsolutePath() + “,文件是否存在:” + fileA1.exists());

System.out.println(“标注路径:” + fileA1.getCanonicalPath() + “,文件是否存在:” + fileA1.exists());

System.out.println(“相对路径:” + fileA1.getPath() + “,文件是否存在:” + fileA1.exists());

控制台打印如下:

绝对路径:D:\AJavaEE\EclipseWorkSpace\PathDemo\src\com\study\path\a.txt,文件是否存在:true

标注路径:D:\AJavaEE\EclipseWorkSpace\PathDemo\src\com\study\path\a.txt,文件是否存在:true

相对路径:src\com\study\path\a.txt,文件是否存在:true

例1.5 参数传”src/b.txt”

File fileB = new File(“src/b.txt”);

System.out.println(“绝对路径:” + fileB.getAbsolutePath() + “,文件是否存在:” + fileB.exists());

System.out.println(“标注路径:” + fileB.getCanonicalPath() + “,文件是否存在:” + fileB.exists());

System.out.println(“相对路径:” + fileB.getPath() + “,文件是否存在:” + fileB.exists());

控制台打印如下:

绝对路径:D:\AJavaEE\EclipseWorkSpace\PathDemo\src\b.txt,文件是否存在:true

标注路径:D:\AJavaEE\EclipseWorkSpace\PathDemo\src\b.txt,文件是否存在:true

相对路径:src\b.txt,文件是否存在:true

例1.6 参数传”c.txt”

File fileC = new File(“c.txt”);

System.out.println(“绝对路径:” + fileC.getAbsolutePath() + “,文件是否存在:” + fileC.exists());

System.out.println(“标注路径:” + fileC.getCanonicalPath() + “,文件是否存在:” + fileC.exists());

System.out.println(“相对路径:” + fileC.getPath() + “,文件是否存在:” + fileC.exists());

控制台打印如下:

绝对路径:D:\AJavaEE\EclipseWorkSpace\PathDemo\c.txt,文件是否存在:true

标注路径:D:\AJavaEE\EclipseWorkSpace\PathDemo\c.txt,文件是否存在:true

相对路径:c.txt,文件是否存在:true

二、利用类加载器来读取文件

例2.1 参数传”“(空串)

URL resource = PathTest.class.getClassLoader().getResource(“”);

String path = resource.getPath();

System.out.println(path);

控制台打印如下:

/D:/AJavaEE/EclipseWorkSpace/PathDemo/bin/

例2.2参数传”com/study/path/a.txt”

URL resource = PathTest.class.getClassLoader().getResource(“com/study/path/a.txt”);

String path = resource.getPath();

System.out.println(path);

控制台打印如下:

/D:/AJavaEE/EclipseWorkSpace/PathDemo/bin/com/study/path/a.txt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值