[Java IO]基础:读取文件

1. 读取文件路径字符串

1.1 利用ClassLoader类方法获得文件路径

System.out.println(this.getClass().getClassLoader().getResource(".").getPath());// 得到的是项目的class文件路径
System.out.println(this.getClass().getClassLoader().getResource("").getPath());// 得到的是项目的class文件路径
System.out.println(this.getClass().getResource("/").getPath());// 得到的是项目的class文件路径
System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());
System.out.println(Thread.currentThread().getContextClassLoader().getResource(".").getPath());
System.out.println(this.getClass().getResource("").getPath());// ★得到当前文件所在的包路径
System.out.println(this.getClass().getResource(".").getPath());//★得到当前文件所在的包路径

结果:
/D:/workspace/DGCertTest/build/classes/
/D:/workspace/DGCertTest/build/classes/
/D:/workspace/DGCertTest/build/classes/
/D:/workspace/DGCertTest/build/classes/
/D:/workspace/DGCertTest/build/classes/
/D:/workspace/DGCertTest/build/classes/com/test/
/D:/workspace/DGCertTest/build/classes/com/test/

1.2 通过getCanonicalPath()getAbsolutePath()获得文件路径

File directory = new File("");// 设定为当前文件夹 
System.out.println(directory.getCanonicalPath());// 获取标准的路径 
System.out.println(directory.getAbsolutePath());// 获取绝对路径 
//结果:D:\workspace\java_base
File panFu = new File("/");// 设定为当前文件夹 
System.out.println(panFu.getCanonicalPath());//得到所在磁盘
System.out.println(directory.getAbsolutePath());// 获取绝对路径 
//结果:D:\

2. 通过文件路径获取IO流

2.1 FileInputStream:读取本地磁盘特定位置的文件

String path=”D:\HwTemp\dongguan_test\test.xml”;
FileInputStream fis=new FileInputStream(new File(path));
但是在这种方式在换个环境测试时又要重新拷贝到别人的磁盘上,没有放到工程中,用工程相对路径来访问方便。
这样工程换个环境能直接运行,即String ->FileInputStream

//工程路径+文件所在工程的相对路径(用)
String path = System.getProperty("user.dir")+"\\src\\file.txt";
//将所有的上面得到的路径中"\"转换成"\\"
path = path.replaceAll("\\\\","\\\\\\\\");
System.out.println(path);
FileInputStream fis = new FileInputStream(new File(path));//D:\\workspace\\java_base\\src\\file.txt

注意:字符串里 \\表示一个”\” 但relaceAll里的 第一个参数是一个正则表达式,在正则里\\\\表示一个”\
System.getProperty("user.dir")Java代码如果放到jsp中,得到的将是IDE的安装路径。所以该代码不能用jsp页面中

2.2 InputStream:方法get****AsStream读取本工程中文件:

该类方法返回InputStream
参考文档:Java读取工程里的文件

  • 方法一:(未验证)将xml文件放在WEB-INF目录下,然后
InputStream is=getServletContext().getResourceAsStream( "/WEB-INF/xmlfile.xml" );
  • 方法二:将xml文件放在/WEB-INF/classes目录下或classpath的jar包中,则可以使用ClassLoader的静态方法getSystemResourceAsStream(String s)读取;
String s_xmlpath="com/spf/web/ext/hotspot/hotspotxml/hotspot.xml";
InputStream in=ClassLoader.getSystemResourceAsStream(s_xmlpath);
  • 方法三:xml在随意某个包路径下
String s_xmlpath="com/spf/web/ext/hotspot/hotspotxml/hotspot.xml";
ClassLoader classLoader=HotspotXmlParser.class.getClassLoader();
InputStream in=classLoader.getResourceAsStream(s_xmlpath);

3. 附件

3.1 附件1:通过System.getProperties()获取系统参数

Properties props=System.getProperties(); //系统属性
System.out.println("Java的运行环境版本:"+props.getProperty("java.version"));
System.out.println("Java的运行环境供应商:"+props.getProperty("java.vendor"));
System.out.println("Java供应商的URL:"+props.getProperty("java.vendor.url"));
System.out.println("Java的安装路径:"+props.getProperty("java.home"));
System.out.println("Java的虚拟机规范版本:"+props.getProperty("java.vm.specification.version"));
System.out.println("Java的虚拟机规范供应商:"+props.getProperty("java.vm.specification.vendor"));
System.out.println("Java的虚拟机规范名称:"+props.getProperty("java.vm.specification.name"));
System.out.println("Java的虚拟机实现版本:"+props.getProperty("java.vm.version"));
System.out.println("Java的虚拟机实现供应商:"+props.getProperty("java.vm.vendor"));
System.out.println("Java的虚拟机实现名称:"+props.getProperty("java.vm.name"));
System.out.println("Java运行时环境规范版本:"+props.getProperty("java.specification.version"));
System.out.println("Java运行时环境规范供应商:"+props.getProperty("java.specification.vender"));
System.out.println("Java运行时环境规范名称:"+props.getProperty("java.specification.name"));
System.out.println("Java的类格式版本号:"+props.getProperty("java.class.version"));
System.out.println("Java的类路径:"+props.getProperty("java.class.path"));
System.out.println("加载库时搜索的路径列表:"+props.getProperty("java.library.path"));
System.out.println("默认的临时文件路径:"+props.getProperty("java.io.tmpdir"));
System.out.println("一个或多个扩展目录的路径:"+props.getProperty("java.ext.dirs"));
System.out.println("操作系统的名称:"+props.getProperty("os.name"));
System.out.println("操作系统的构架:"+props.getProperty("os.arch"));
System.out.println("操作系统的版本:"+props.getProperty("os.version"));
System.out.println("文件分隔符:"+props.getProperty("file.separator"));   //在 unix 系统中是"/"
System.out.println("路径分隔符:"+props.getProperty("path.separator"));   //在 unix 系统中是":"
System.out.println("行分隔符:"+props.getProperty("line.separator"));   //在 unix 系统中是"/n"
System.out.println("用户的账户名称:"+props.getProperty("user.name"));
System.out.println("用户的主目录:"+props.getProperty("user.home"));
System.out.println("用户的当前工作目录:"+props.getProperty("user.dir"));

3.2 附件2:最基本的路径转换

@Test
public void test_getXmlPath() {
    String path = Thread.currentThread().getContextClassLoader().getResource("").toString();
    path = path.replace('/', '\\'); // 将/换成\
    path = path.replace("file:", ""); // 去掉file:
    path = path.replace("classes\\", ""); // 去掉class\
    path = path.substring(1); // 去掉第一个\,如 \D:\JavaWeb...
    path += "web.xml";
    System.out.println(path);
}
@Test
public void test_getXmlPath() {
    String path = System.getProperty("user.dir")+"\\src\\file.txt";
    System.out.println();
    //★将所有的上面得到的路径中"\"转换成"\\"
    path = path.replaceAll("\\\\","\\\\\\\\");
    System.out.println(path);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值