Java获取资源路径——(八)
获取文件资源有两种方式:
第一种是:
获取Java项目根目录开始制定文件夹下指定文件,不用类加载器(目录开始要加/)
// 获取工程路径
System.out.println(System.getProperty("user.dir"));
// E:\EclipseWorkspace\AllTest // 获取工程路径下的文件
String path = System.getProperty("user.dir") + "\\test.txt";
System.out.println(path);
// E:\EclipseWorkspace\AllTest\test.txt // 获取工程指定文件夹下的文件(第一个目录加/)
String path1 = System.getProperty("user.dir") + "\\src\\txtFile\\test.txt";
System.out.println(path1);
// E:\EclipseWorkspace\AllTest\src\txtFile\test.txt
第二种是:
通过类加载器获取:(path为相对ClassPath的路径,从ClassPath根下获取,不能以“/”开头)。
// 获取classpath路径
System.out.println(ClassLoaderTest.class.getClassLoader().getResource(""));
// file:/E:/EclipseWorkspace/AllTest/bin/ // 获取classpath路径下txtFile文件夹指定内容(第一个目录不加/)
String path1 = ClassLoaderTest.class.getClassLoader().getResource("txtFile/test.txt").getPath();
System.out.println(path1);
// /E:/EclipseWorkspace/AllTest/bin/txtFile/test.txt // 获取classpath路径下指定内容
String path2 = ClassLoaderTest.class.getClassLoader().getResource("test.txt").getPath();
System.out.println(path2);
// /E:/EclipseWorkspace/AllTest/bin/test.txt
-----------------------------------------------------------------------测试程序--------------------------------------------------------
package Java_Test; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import org.junit.Test; /**
* 测试类加载器读取资源
*
* @author: qlq
* @date : 2017年8月2日上午9:19:22
*/
public class ClassLoaderTest { // Java同过类加载器读取资源路径问题
@Test
public void test1() throws IOException {
// 获取classpath路径
System.out.println(ClassLoaderTest.class.getClassLoader().getResource(""));
// file:/E:/EclipseWorkspace/AllTest/bin/ // 获取classpath路径下txtFile文件夹指定内容(第一个目录不加/)
String path1 = ClassLoaderTest.class.getClassLoader().getResource("txtFile/test.txt").getPath();
System.out.println(path1);
// /E:/EclipseWorkspace/AllTest/bin/txtFile/test.txt // 获取classpath路径下指定内容
String path2 = ClassLoaderTest.class.getClassLoader().getResource("test.txt").getPath();
System.out.println(path2);
// /E:/EclipseWorkspace/AllTest/bin/test.txt
} // 通过系统的绝对路径获取,先获取工程目录,再获取工程目录下指定的文件
@Test
public void fun2() {
// 获取工程路径
System.out.println(System.getProperty("user.dir"));
// E:\EclipseWorkspace\AllTest // 获取工程路径下的文件
String path = System.getProperty("user.dir") + "\\test.txt";
System.out.println(path);
// E:\EclipseWorkspace\AllTest\test.txt // 获取工程指定文件夹下的文件(第一个目录加/)
String path1 = System.getProperty("user.dir") + "\\src\\txtFile\\test.txt";
System.out.println(path1);
// E:\EclipseWorkspace\AllTest\src\txtFile\test.txt } // 读取src文件夹下的文件(txtFile从classpath下开始,前面无/)
@Test
public void test3() throws IOException {
// txtFile代表src下面的一个文件夹
String path = ClassLoaderTest.class.getClassLoader().getResource("txtFile/test.txt").getPath();
System.out.println(path);
// /E:/EclipseWorkspace/AllTest/bin/txtFile/test.txt
InputStream inputStream = new FileInputStream(new File(path));
int value1;
while ((value1 = inputStream.read()) != -1) { // 读取文件
System.out.print((char) value1); // www
}
} // 直接以流的形式读取获取
@Test
public void test4() throws IOException {
InputStream resourceAsStream = ClassLoaderTest.class.getClassLoader().getResourceAsStream("Java_Test/test.txt");
int value;
while ((value = resourceAsStream.read()) != -1) { // 读取文件
System.out.print((char) value);
} } }
Java获取资源路径——(八)的更多相关文章
- Java获取资源的路径
在Java中,有两种路径: 类路径 文件夹路径 使用类路径有两种方式: object.getClass().getResource()返回资源的URL MyClass.class.getResourc ...
- javaWeb项目中的路径格式 请求url地址 客户端路径 服务端路径 url-pattern 路径 获取资源路径 地址 url
javaweb项目中有很多场景的路径客户端的POST/GET请求,服务器的请求转发,资源获取需要设置路径等这些路径表达的含义都有不同,所以想要更好的书写规范有用的路径代码 需要对路径有一个清晰地认知 ...
- Eclipse获取资源路径
一.问题: 这几天做一个单机版的数据抓取项目,之前都加载了spring或者是maven 使用[this.getClass().getClassLoader().getResource("ma ...
- IDEA中获取资源路径问题
更正 以src开始,就能用相对路径了... shift+ctrl+alt+s 调出项目结构, 在Modules里,就是设置 Sources Resources Test的界面, 右面的路径就是相对路径 ...
- java获取类加载路径和项目根路径的5种方法
// 第一种:获取类加载的根路径 D:\IDEAWorkspace\hs-bluetooth-lock\src\applications\bluetooth-api\target\classes Fi ...
- Java获取资源文件
比如我们有以下目录 |--project |--src |--javaapplication |--Test.java |--file1.txt |--file2.txt |--build |--ja ...
- JAVA获取webapp路径
1.使用ServletContext获取webapp目录 在Servlet中 String path = getServletContext().getRealPath("/"); ...
- java 获取类路径
package com.jason.test; import java.io.File; import java.io.IOException; import java.net.URL; public ...
- Java获取各种路径
(1).request.getRealPath("/");//不推荐使用获取工程的根路径(2).request.getRealPath(request.getRequestURI( ...
随机推荐
- hdu1176 (免费馅饼)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- cocos2d-x入门学习笔记,主要介绍cocos2d-x的基本结构,并且介绍引擎自带的示例
cocos2d-x 3.0 制作横版格斗游戏 http://philon.cn/post/cocos2d-x-3.0-zhi-zuo-heng-ban-ge-dou-you-xi http://blo ...
- [CF1091D]New Year and the Permutation Concatenation
link 题目大意 给$n!$个$n$的排列,按字典序从小到大连成一条序列,例如$3$的情况为:$[1,2,3, 1,3,2, 2,1,3 ,2,3,1 ,3,1,2 ,3,2,1]$,问其中长度为$ ...
- C++11并发——多线程std::mutex (二)
https://www.cnblogs.com/haippy/p/3237213.html Mutex 又称互斥量,C++ 11中与 Mutex 相关的类(包括锁类型)和函数都声明在 <mute ...
- Codeforces Round #514 (Div. 2) D. Nature Reserve
http://codeforces.com/contest/1059/problem/D 最大值: 最左下方和最右下方分别有一个点 r^2 - (r-1)^2 = (10^7)^2 maxr<0 ...
- 2018.9青岛网络预选赛(J)
传送门:Problem J https://www.cnblogs.com/violet-acmer/p/9664805.html 题目大意: BaoBao和DreamGrid玩游戏,轮流按灯的按钮, ...
- 2156: 中南大学2018年ACM暑期集训前期训练题集(入门题) D: 机器人的指令
不要用gets!不要用gets!不要用gets! 不要用gets!不要用gets!不要用gets! 不要用gets!不要用gets!不要用gets! 不要用gets!不要用gets!不要用gets! ...
- @RequestBody
之前写过一篇记录文章,写的是将一个比较复杂的数据结构在前台组合起来后传递到后台. 当时并不太了解@RequestBody,也并没有使用js提供的JSON.stringify()方法 所有都是自己写的, ...
- MUI 窗体切换(setting设置)即窗口从右往左切换,返回从左往右切换。
1)引入mui.min.css 2)引入mui.min.js 引入mui.view.js 1.HTML: //这是页面的主体结构 <div id="app" class=& ...
- context configure and clock schedule
每个窗口都有自己的context,这里演示怎么配置context以及如何实现定时器...... #-*- coding:gbk -*- import pyglet platform=pyglet.wi ...