Java获取资源路径——(八)

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

第一种是:

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

 1         // 获取工程路径
 2         System.out.println(System.getProperty("user.dir"));
 3         // E:EclipseWorkspaceAllTest
 4 
 5         // 获取工程路径下的文件
 6         String path = System.getProperty("user.dir") + "\test.txt";
 7         System.out.println(path);
 8         // E:EclipseWorkspaceAllTest	est.txt
 9 
10         // 获取工程指定文件夹下的文件(第一个目录加/)
11         String path1 = System.getProperty("user.dir") + "\src\txtFile\test.txt";
12         System.out.println(path1);
13         // E:EclipseWorkspaceAllTestsrc	xtFile	est.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:EclipseWorkspaceAllTest
43 
44         // 获取工程路径下的文件
45         String path = System.getProperty("user.dir") + "\test.txt";
46         System.out.println(path);
47         // E:EclipseWorkspaceAllTest	est.txt
48 
49         // 获取工程指定文件夹下的文件(第一个目录加/)
50         String path1 = System.getProperty("user.dir") + "\src\txtFile\test.txt";
51         System.out.println(path1);
52         // E:EclipseWorkspaceAllTestsrc	xtFile	est.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 }
原文地址:https://www.cnblogs.com/qlqwjy/p/7272821.html