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

来源:互联网 发布:ppt课件下载软件 编辑:程序博客网 时间:2024/06/08 04:55

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


这里写图片描述

一、利用创建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

原创粉丝点击