resources
この記事では、ディレクトリ内のファイルを取得する 9 つの方法を説明します。ファイルを印刷する方法は次のとおりです。
/**
* 根据文件路径读取文件内容
*
* @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();
}
方法 1
主要なコアメソッドは us getResource
e とgetPath
メソッドです。ここにはgetResource("")
空の文字列があります。
public void function1(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource("").getPath();//注意getResource("")里面是空字符串
System.out.println(path);
String filePath = path + fileName;
System.out.println(filePath);
getFileContent(filePath);
}
方法2
主なコアメソッドは、getResource
およびgetPath
メソッドを使用してgetResource(fileName)
、メソッドを通じてファイルパスを直接取得することです。 である場合は注意してください路径中带有中文一定要使用URLDecoder.decode解码
。
/**
* 直接通过文件名getPath来获取路径
*
* @param fileName
* @throws IOException
*/
public void function2(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getPath();//注意getResource("")里面是空字符串
System.out.println(path);
String filePath = URLDecoder.decode(path, "UTF-8");//如果路径中带有中文会被URLEncoder,因此这里需要解码
System.out.println(filePath);
getFileContent(filePath);
}
方法3
ファイル名 + getFile() を使用してファイルを直接取得します。ファイル パスの場合、getFile と getPath は同じ効果を持ちます。URL パスの場合、getPath はパラメータ付きのパスです。次のように:
url.getFile()=/word/test.docx?id=123
url.getPath()=/word/test.docx
getFile() を使用してファイルを取得するコードは次のとおりです。
/**
* 直接通过文件名+getFile()来获取
*
* @param fileName
* @throws IOException
*/
public void function3(String fileName) throws IOException {
String path = this.getClass().getClassLoader().getResource(fileName).getFile();//注意getResource("")里面是空字符串
System.out.println(path);
String filePath = URLDecoder.decode(path, "UTF-8");//如果路径中带有中文会被URLEncoder,因此这里需要解码
System.out.println(filePath);
getFileContent(filePath);
}
方法 4 (重要)
getResourceAsStream
ストリームを取得するメソッドを直接使用する 上記のメソッドはいずれもファイルパスを取得する必要がありますが、SpringBoot ではすべてのファイルが jar パッケージ内にあり、実際のパスがないため、次のメソッドが使用できます。
/**
* 直接使用getResourceAsStream方法获取流
* springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件
*
* @param fileName
* @throws IOException
*/
public void function4(String fileName) throws IOException {
InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
getFileContent(in);
}
方法5 (重要)
主な方法はgetResourceAsStream
メソッドを使用してフローを取得することです。それを使用しない場合は、リソースのルートパスから直接取得できます。SpringBoot 内のすべてのファイルは jar パッケージ内にあります。実際のパスはありませんgetClassLoader
。getResourceAsStream("/test.txt")
したがって、次の方法が使用できます。
/**
* 直接使用getResourceAsStream方法获取流
* 如果不使用getClassLoader,可以使用getResourceAsStream("/test.txt")直接从resources根路径下获取
*
* @param fileName
* @throws IOException
*/
public void function5(String fileName) throws IOException {
InputStream in = this.getClass().getResourceAsStream("/" + fileName);
getFileContent(in);
}
方法6 (重要)
ClassPathResource
クラスを通じてファイルストリームを取得します。SpringBoot 内のすべてのファイルは jar パッケージ内にあり、実際のパスはないため、次のメソッドを使用できます。
/**
* 通过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 {
String rootPath = System.getProperty("user.dir");//E:\WorkSpace\Personal\example
String filePath = rootPath + "\\example01\src\\main\\resources\\"+fileName;
getFileContent(filePath);
}
道八
現在の絶対パスを取得してもnew File("")
、それはローカルの絶対パスのみであり、サーバの取得には使用できません。
/**
* 通过绝对路径获取项目中文件的位置(不能用于服务器)
* @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 + "\\example01\\src\\main\\resources\\"+fileName;
getFileContent(filePath);
}
ウェイナイン
主に環境変数の設定でファイルを環境変数に置き、原理も絶対パスで取得します。
この例では、環境変数を設定します。TEST_ROOT=E:\\WorkSpace\\Personal\\example
System.getenv("TEST_ROOT");
System.getProperty("TEST_ROOT")
環境変数を設定し、絶対パスでファイルを取得します。
/**
* 通过绝对路径获取项目中文件的位置
*
* @param fileName
* @throws IOException
*/
public void function9(String fileName) throws IOException {
System.setProperty("TEST_ROOT","E:\\WorkSpace\\Personal\\example");
//参数为空
String rootPath = System.getProperty("TEST_ROOT");
System.out.println(rootPath);
String filePath = rootPath + "\\example01\\src\\main\\resources\\"+fileName;
getFileContent(filePath);
}