获取文件资源有两种方式:

第一种是:

获取Java项目根目录开始制定文件夹下指定文件,不用类加载器(目录开始要加/)

  1. // 获取工程路径
  2. System.out.println(System.getProperty("user.dir"));
  3. // E:\EclipseWorkspace\AllTest
  4.  
  5. // 获取工程路径下的文件
  6. String path = System.getProperty("user.dir") + "\\test.txt";
  7. System.out.println(path);
  8. // E:\EclipseWorkspace\AllTest\test.txt
  9.  
  10. // 获取工程指定文件夹下的文件(第一个目录加/)
  11. String path1 = System.getProperty("user.dir") + "\\src\\txtFile\\test.txt";
  12. System.out.println(path1);
  13. // E:\EclipseWorkspace\AllTest\src\txtFile\test.txt

第二种是:

  通过类加载器获取:(path为相对ClassPath的路径,从ClassPath根下获取,不能以“/”开头)。

  1. // 获取classpath路径
  2. System.out.println(ClassLoaderTest.class.getClassLoader().getResource(""));
  3. // file:/E:/EclipseWorkspace/AllTest/bin/
  4.  
  5. // 获取classpath路径下txtFile文件夹指定内容(第一个目录不加/)
  6. String path1 = ClassLoaderTest.class.getClassLoader().getResource("txtFile/test.txt").getPath();
  7. System.out.println(path1);
  8. // /E:/EclipseWorkspace/AllTest/bin/txtFile/test.txt
  9.  
  10. // 获取classpath路径下指定内容
  11. String path2 = ClassLoaderTest.class.getClassLoader().getResource("test.txt").getPath();
  12. System.out.println(path2);
  13. // /E:/EclipseWorkspace/AllTest/bin/test.txt

  

-----------------------------------------------------------------------测试程序--------------------------------------------------------

  1. package Java_Test;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8.  
  9. import org.junit.Test;
  10.  
  11. /**
  12. * 测试类加载器读取资源
  13. *
  14. * @author: qlq
  15. * @date : 2017年8月2日上午9:19:22
  16. */
  17. public class ClassLoaderTest {
  18.  
  19. // Java同过类加载器读取资源路径问题
  20. @Test
  21. public void test1() throws IOException {
  22. // 获取classpath路径
  23. System.out.println(ClassLoaderTest.class.getClassLoader().getResource(""));
  24. // file:/E:/EclipseWorkspace/AllTest/bin/
  25.  
  26. // 获取classpath路径下txtFile文件夹指定内容(第一个目录不加/)
  27. String path1 = ClassLoaderTest.class.getClassLoader().getResource("txtFile/test.txt").getPath();
  28. System.out.println(path1);
  29. // /E:/EclipseWorkspace/AllTest/bin/txtFile/test.txt
  30.  
  31. // 获取classpath路径下指定内容
  32. String path2 = ClassLoaderTest.class.getClassLoader().getResource("test.txt").getPath();
  33. System.out.println(path2);
  34. // /E:/EclipseWorkspace/AllTest/bin/test.txt
  35. }
  36.  
  37. // 通过系统的绝对路径获取,先获取工程目录,再获取工程目录下指定的文件
  38. @Test
  39. public void fun2() {
  40. // 获取工程路径
  41. System.out.println(System.getProperty("user.dir"));
  42. // E:\EclipseWorkspace\AllTest
  43.  
  44. // 获取工程路径下的文件
  45. String path = System.getProperty("user.dir") + "\\test.txt";
  46. System.out.println(path);
  47. // E:\EclipseWorkspace\AllTest\test.txt
  48.  
  49. // 获取工程指定文件夹下的文件(第一个目录加/)
  50. String path1 = System.getProperty("user.dir") + "\\src\\txtFile\\test.txt";
  51. System.out.println(path1);
  52. // E:\EclipseWorkspace\AllTest\src\txtFile\test.txt
  53.  
  54. }
  55.  
  56. // 读取src文件夹下的文件(txtFile从classpath下开始,前面无/)
  57. @Test
  58. public void test3() throws IOException {
  59. // txtFile代表src下面的一个文件夹
  60. String path = ClassLoaderTest.class.getClassLoader().getResource("txtFile/test.txt").getPath();
  61. System.out.println(path);
  62. // /E:/EclipseWorkspace/AllTest/bin/txtFile/test.txt
  63. InputStream inputStream = new FileInputStream(new File(path));
  64. int value1;
  65. while ((value1 = inputStream.read()) != -1) { // 读取文件
  66. System.out.print((char) value1); // www
  67. }
  68. }
  69.  
  70. // 直接以流的形式读取获取
  71. @Test
  72. public void test4() throws IOException {
  73. InputStream resourceAsStream = ClassLoaderTest.class.getClassLoader().getResourceAsStream("Java_Test/test.txt");
  74. int value;
  75. while ((value = resourceAsStream.read()) != -1) { // 读取文件
  76. System.out.print((char) value);
  77. }
  78.  
  79. }
  80.  
  81. }

Java获取资源路径——(八)的更多相关文章

  1. Java获取资源的路径

    在Java中,有两种路径: 类路径 文件夹路径 使用类路径有两种方式: object.getClass().getResource()返回资源的URL MyClass.class.getResourc ...

  2. javaWeb项目中的路径格式 请求url地址 客户端路径 服务端路径 url-pattern 路径 获取资源路径 地址 url

    javaweb项目中有很多场景的路径客户端的POST/GET请求,服务器的请求转发,资源获取需要设置路径等这些路径表达的含义都有不同,所以想要更好的书写规范有用的路径代码 需要对路径有一个清晰地认知 ...

  3. Eclipse获取资源路径

    一.问题: 这几天做一个单机版的数据抓取项目,之前都加载了spring或者是maven 使用[this.getClass().getClassLoader().getResource("ma ...

  4. IDEA中获取资源路径问题

    更正 以src开始,就能用相对路径了... shift+ctrl+alt+s 调出项目结构, 在Modules里,就是设置 Sources Resources Test的界面, 右面的路径就是相对路径 ...

  5. java获取类加载路径和项目根路径的5种方法

    // 第一种:获取类加载的根路径 D:\IDEAWorkspace\hs-bluetooth-lock\src\applications\bluetooth-api\target\classes Fi ...

  6. Java获取资源文件

    比如我们有以下目录 |--project |--src |--javaapplication |--Test.java |--file1.txt |--file2.txt |--build |--ja ...

  7. JAVA获取webapp路径

    1.使用ServletContext获取webapp目录 在Servlet中 String path = getServletContext().getRealPath("/"); ...

  8. java 获取类路径

    package com.jason.test; import java.io.File; import java.io.IOException; import java.net.URL; public ...

  9. Java获取各种路径

    (1).request.getRealPath("/");//不推荐使用获取工程的根路径(2).request.getRealPath(request.getRequestURI( ...

随机推荐

  1. 睡前小dp-hdu3853-概率dp

    http://acm.hdu.edu.cn/showproblem.php?pid=3853 膜猴烧酒madoka 讲定义为dp[i][j] 位置为ij的魔法值期望,可以发现dp[i][j] = dp ...

  2. xml文件格式化后不能获取到值

    在有些时候,我们要使用到xml文件,必须得将文件中的内容压缩成一行,才能读取到其中的值,一旦有换行符.制表符.空格之类的就读不到.所以只能在开发好以后,将代码压缩再执行,十分不方便. 尝试了几个替换符 ...

  3. 自学Zabbix之路15.1 Zabbix数据库表结构简单解析-Hosts表、Hosts_groups表、Interface表

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix之路15.1 Zabbix数据库表结构简单解析-Hosts表.Hosts_grou ...

  4. Android GPS定位测试(附效果图)

    今天因为工作需要,把以前编写的一个GPS测试程序拿出来重新修改了一下.这个程序说起来有些历史了,是我11年编写的,那时候学了Android开发没多久,算是一个实验性的作品.现在工作需要,重新拿出来修整 ...

  5. Shell基础知识(四)

    字符串详解 字符串可以由 单引号/双引号/无引号 包围.如下所示 >> str1=hello str2="hello" str3='hello' << 三种 ...

  6. Eclipse 插件Maven在使用 add dependency,找不到包,解决办法

    通过右键单击pom.xml文件选择maven –> add dependency 或者是打开pom.xml文件,选择dependencies –>add 时,搜索不到依赖的jar包,解决方 ...

  7. 爬楼梯问题 leetcode70

    假设你正在爬楼梯,需要n阶你才能到达楼顶,n是正整数 每次你可以爬1或2个台阶,有多少种不同的方法可以爬到楼顶 当n=1时,steps=1 当n=2时,1+1,2 steps=2 当n=3时,1+1+ ...

  8. gcc编译器命令使用详解

    1.gcc包含的c/c++编译器gcc,cc,c++,g++,gcc和cc是一样的,c++和g++是一样的,(没有看太明白前面这半句是什么意思:))一般c程序就用gcc编译,c++程序就用g++编译 ...

  9. 【POJ1151】Atlantis

    题目大意:给定 N 个矩形,求这些矩形的面积并. 题解:采用扫描线算法. 首先,按照矩形的横坐标排序,在纵坐标方向上维护一根扫描线被覆盖的长度,在这里采用线段树维护.统计答案时,从左到右扫描 2N 个 ...

  10. [luoguU42591][小T的绝对值]

    luoguU42592 20分思路 对给出的序列求出前缀和,然后\(n^2\)暴力枚举即可拿到第一档分 40分思路 对于数列中的数都相同的情况.只需要特判即可.只要特别注意全都是0的情况即可. 100 ...