当前位置:网站首页>Read the resources files in the directory path
Read the resources files in the directory path
2022-08-06 05:03:00 【ljt-tiger】
读取 resources 目录下的文件路径
package com.exe.shell.util;
import org.springframework.core.io.ClassPathResource;
import java.io.*;
import java.net.URLDecoder;
/** * @description: 读取 resources 目录下的文件路径 * @author: tiger * @create: 2022-07-31 23:24 */
public class FilePath {
/** * 根据文件路径读取文件内容 * * @param fileInPath * @throws IOException */
public static void getFileContent(Object fileInPath) throws IOException {
BufferedReader br = null;
if (fileInPath == null) {
return;
}
if (fileInPath instanceof String) {
br = new BufferedReader(new FileReader(new File((String) fileInPath)));
} else if (fileInPath instanceof InputStream) {
br = new BufferedReader(new InputStreamReader((InputStream) fileInPath));
}
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
public void function1(String fileName) throws IOException {
// 注意getResource("")里面是空字符串
String path = this.getClass().getClassLoader().getResource("").getPath();
System.out.println("path = " + path);
String filePath = path + fileName;
System.out.println("filePath = " + filePath);
getFileContent(filePath);
}
/** * 直接通过文件名getPath来获取路径 * * @param fileName * @throws IOException */
public void function2(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getPath();
System.out.println(path);
// 如果路径中带有中文会被URLEncoder,因此这里需要解码
String filePath = URLDecoder.decode(path, "UTF-8");
System.out.println(filePath);
getFileContent(filePath);
}
/** * 直接通过文件名+getFile()来获取 * * @param fileName * @throws IOException */
public void function3(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getFile();
System.out.println(path);
// 如果路径中带有中文会被URLEncoder,因此这里需要解码
String filePath = URLDecoder.decode(path, "UTF-8");
System.out.println(filePath);
getFileContent(filePath);
}
/** * 重要常用 * 直接使用getResourceAsStream方法获取流 * springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件 * * @param fileName * @throws IOException */
public void function4(String fileName) throws IOException {
InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
getFileContent(in);
}
/** * 重要常用 * 直接使用getResourceAsStream方法获取流 * 如果不使用getClassLoader, * 可以使用getResourceAsStream("/配置测试.txt")直接从resources根路径下获取 * * @param fileName * @throws IOException */
public void function5(String fileName) throws IOException {
InputStream in = this.getClass().getResourceAsStream("/" + fileName);
getFileContent(in);
}
/** * 通过ClassPathResource类获取,建议SpringBoot中使用 * springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件 * * @param fileName * @throws IOException */
public void function6(String fileName) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(fileName);
InputStream inputStream = classPathResource.getInputStream();
getFileContent(inputStream);
}
/** * 通过绝对路径获取项目中文件的位置(不能用于服务器) * * @param fileName * @throws IOException */
public void function7(String fileName) throws IOException {
//E:\WorkSpace\Git\spring-framework-learning-example
String rootPath = System.getProperty("user.dir");
String filePath = rootPath +
"\\exe-shell\\src\\main\\resources\\"
+ fileName;
System.out.println("path = " + rootPath);
// getFileContent(filePath);
}
/** * 通过绝对路径获取项目中文件的位置(不能用于服务器) * * @param fileName * @throws IOException */
public void function8(String fileName) throws IOException {
//参数为空
File directory = new File("");
//规范路径:getCanonicalPath() 方法返回绝对路径,会把 ..\ 、.\ 这样的符号解析掉
String rootCanonicalPath = directory.getCanonicalPath();
//绝对路径:getAbsolutePath() 方法返回文件的绝对路径,如果构造的时候是全路径就直接返回全路径,如果构造时是相对路径,就返回当前目录的路径 + 构造 File 对象时的路径
String rootAbsolutePath = directory.getAbsolutePath();
System.out.println(rootCanonicalPath);
System.out.println(rootAbsolutePath);
String filePath = rootCanonicalPath + "\\exe-shell\\src\\main\\resources\\" + fileName;
// getFileContent(filePath);
}
/** * 通过绝对路径获取项目中文件的位置 * * @param fileName * @throws IOException */
public void function9(String fileName) throws IOException {
System.setProperty("TEST_ROOT","C:\\AAAAA\\all-code\\exe-shell");
//参数为空
String rootPath = System.getProperty("TEST_ROOT");
System.out.println(rootPath);
String filePath = rootPath + "\\exe-shell\\src\\main\\resources\\"+fileName;
// getFileContent(filePath);
}
public static void main(String[] args) throws IOException {
// String pathName = "ip2region/ip2region.xdb";
String pathName = "ip2region/tiger.text";
FilePath filePath = new FilePath();
// filePath.function1(pathName);
// filePath.function2(pathName);
// filePath.function3(pathName);
// filePath.function4(pathName);
// filePath.function5(pathName);
// filePath.function6(pathName);
filePath.function7(pathName);
// filePath.function8(pathName);
filePath.function9(pathName);
}
}
边栏推荐
- 1403. 非递增顺序的最小子序列
- "Module + antenna" full stack solution, to speed up the Internet of things terminal efficient deployment
- 洛谷: P1057 [NOIP2008 普及组] 传球游戏
- aws篇8
- Detailed BGP (1)
- aws篇7
- [C language array subscript out of bounds] Infinite loop caused by array subscript out of bounds
- 【图像分类】2021-Twins NeurIPS
- 移除超出规定List最大数量的数据
- LoRa技术有哪些应用场景?
猜你喜欢

"Module + antenna" full stack solution, to speed up the Internet of things terminal efficient deployment

7. Building RESTful Services

Practical and bytes to beat Flink status query optimization

跨境电商风险之账号风险如何避免?有什么解决方案?

Responsive dream weaving template diving and swimming website

数学建模学习(77):matlab相关性分析(皮尔逊,肯德尔,斯皮尔曼)

Responsive dream weaving template wire mesh website

2022年最新辽宁建筑安全员模拟题库及答案

离散信源最大熵定理的Matlab实现

Industrial Robot Review Questions
随机推荐
动态路由协议的分类、动静态路由优缺点、RIP简介、组播单播广播详解(附图)
dedecms collection selection content model display complete
旁路缓存策略的缓存一致性问题?
MQ基本概念详解
论文阅读 (65):RePaint: Inpainting using Denoising Diffusion Probabilistic Models
[Binary tree] The number of nodes whose statistical value is equal to the average value of the subtree
线程安全的随机数
ESP 特权隔离机制—案例研究
[C language array subscript out of bounds] Infinite loop caused by array subscript out of bounds
el-table样式修改
响应式织梦模板企业集团类网站
623. Add a row to a binary tree
Practical and bytes to beat Flink status query optimization
温度敏感/PH敏感/电场敏感/温度/pH双重敏感/磁场敏感型水凝胶的制备
OSPF综合实验:
extends继承
Datax and Datax-web download and use
dedecms采集选择内容模型显示完整
Pytorch入门实战(5):基于nn.Transformer实现机器翻译(英译汉)
The difference between defineProperty and proxy