读取 resources 目录下的文件路径
package com.exe.shell.util;
import org.springframework.core.io.ClassPathResource;
import java.io.*;
import java.net.URLDecoder;
public class FilePath {
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 {
String path = this.getClass().getClassLoader().getResource("").getPath();
System.out.println("path = " + path);
String filePath = path + fileName;
System.out.println("filePath = " + filePath);
getFileContent(filePath);
}
public void function2(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getPath();
System.out.println(path);
String filePath = URLDecoder.decode(path, "UTF-8");
System.out.println(filePath);
getFileContent(filePath);
}
public void function3(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getFile();
System.out.println(path);
String filePath = URLDecoder.decode(path, "UTF-8");
System.out.println(filePath);
getFileContent(filePath);
}
public void function4(String fileName) throws IOException {
InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
getFileContent(in);
}
public void function5(String fileName) throws IOException {
InputStream in = this.getClass().getResourceAsStream("/" + fileName);
getFileContent(in);
}
public void function6(String fileName) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(fileName);
InputStream inputStream = classPathResource.getInputStream();
getFileContent(inputStream);
}
public void function7(String fileName) throws IOException {
String rootPath = System.getProperty("user.dir");
String filePath = rootPath +
"\\exe-shell\\src\\main\\resources\\"
+ fileName;
System.out.println("path = " + rootPath);
}
public void function8(String fileName) throws IOException {
File directory = new File("");
String rootCanonicalPath = directory.getCanonicalPath();
String rootAbsolutePath = directory.getAbsolutePath();
System.out.println(rootCanonicalPath);
System.out.println(rootAbsolutePath);
String filePath = rootCanonicalPath + "\\exe-shell\\src\\main\\resources\\" + fileName;
}
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;
}
public static void main(String[] args) throws IOException {
String pathName = "ip2region/tiger.text";
FilePath filePath = new FilePath();
filePath.function7(pathName);
filePath.function9(pathName);
}
}