Ⅰ maven項目只能讀到絕對路徑下的文件,讀不到相對路徑下的文件
新建一個maven工程後,main目錄下會有java和resources兩個文件夾,其中java文件夾下存放源代碼,resources文件夾下存放一些配置文件等。
maven%E5%B7%A5%E7%A8%8B%E7%BB%93%E6%9E%84%E5%9B%BE.pngJava: maven下讀取資源文件的問題, java路徑
在弄清楚編譯後,資源文件以及位元組碼存在哪裡這個問題之前,有必要明白什麼是classpath
classpath實際上就是編譯後的 以 classes 文件夾為起點的路徑,而在ItelliJ IDEA 中編譯後的文件都會存入out/proction下。
所以,編譯後,resources文件夾中的文件以及java目錄下的文件都會存入同一個目錄(out/proction)下,也就是說,編譯後是不存在java和resources這兩個目錄的。
讀取資源文件的若干中方法
package me.shenchao.main;
import java.io.*;
/** * Created by jerry on 16-4-20. */
public class Demo1 {
private static void readTxt(String filePath) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filePath));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
//獲取classpath路徑
System.out.println("classpath路徑: "+Demo1.class.getClassLoader().getResource("").getPath());
//獲取當前類的載入路徑
System.out.println("當前類載入路徑: "+Demo1.class.getResource("").getPath());
// 讀取文件resources目錄中文件的若干種方法
// 方法一:從classpath路徑出發讀取
readTxt(Demo1.class.getClassLoader().getResource("test/demo1.txt").getPath());
// 方法二:從類載入路徑出發,相當於使用絕對路徑
readTxt(Demo1.class.getResource("/test/demo1.txt").getPath());
// 方法三:從類載入路徑出發,相當於使用相對路徑
readTxt(Demo1.class.getResource("../../../test/demo1.txt").getPath());
}
}
其中demo1.txt文件中內容為:
hahahahahahahahahha
輸出如下:
classpath路徑: /home/jerry/IdeaProjects/Demo/out/proction/demo1/
當前類載入路徑: /home/jerry/IdeaProjects/Demo/out/proction/demo1/me/shenchao/main/
hahahahahahahahahha
hahahahahahahahahha
hahahahahahahahahha
從上面可以發現getResource 與 class.getClassLoader().getResource兩者的區別:
前者獲取的是當前類載入的路徑,如果用此方法讀取文件則有兩種方法,與相對路徑絕對路徑非常類似,具體參見代碼
後者獲取的是類載入器的路徑,即會到classpath路徑下。可以理解當前在 classp/ 目錄下,要想訪問哪個文件,直接填寫路徑即可,不用區分相對路徑和絕對路徑。顯然,此方法比較容易寫出。推薦。
.gif
相關
Related Posts
JAVA: 理解Java中的類初始化
在運行 Java 代碼時,很多時候需要弄清楚程序執行的流程,而面向對象的 Java 程序並非像主要為面向過程而設計的 C 語言一樣去順序執行(簡單按照代碼的順序),這使得對於類文件的載入以及執行流程的理解非常重要。本文簡單介紹了 Java 類的初始化部分具體過程,包括成員變數、靜態代碼塊、構造函數等的初始化時機及執行流程。 初始化時機 根據 javase 8 的文檔說明[1],一個類(本文暫不考慮介面)T…
JAVA: 獲取當前月份c.get(Calendar.MONTH)中月份少一個月
@Test public void testGetTitle(){ System.out.println(new LocalDate().minusDays(1).toString("MM/dd/yyyy")); // joda-time Calendar c1 = Calendar.getInstance(); int year…
JAVA: 讀寫文件的幾種方法
如果您使用java8,可以也參考這篇文章:JAVA: Java8流逐行讀取文件 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat;…
Ⅱ 一個maven項目,想在java程序中讀取pom.xml文件中的version、artifactId等屬性
用dom直接解析pom.xml就可以了。
Ⅲ maven把java項目打包,如何把配置文件提出來
你好:這個把配置文件提取出來可以再pm.xml裡面配置java項目文件路徑下文件打包方式來實現。舉例如下,參考下。
<?xmlversion="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.tmtpt</groupId>
<artifactId>tmrpt</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>tmrpt-client</artifactId>
<version>2.2.2</version>
<name>vpetl-client</name>
<url>www.tmrpt.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>tmrpt-client</finalName>
<defaultGoal>install</defaultGoal>
<plugins>
<!--<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version><configuration><source>1.6</source><target>1.6</target>
<encoding>UTF-8</encoding></configuration></plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.vprisk.main.EtlClientStart</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${basedir}/target/classes</classesDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<outputDirectory>${basedir}/target/maven-archiver</outputDirectory>
<excludes>
<exclude>*.conf</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
<!--拷貝依賴的jar包到lib目錄-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>-dependencies</id>
<phase>package</phase>
<goals>
<goal>-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/maven-archiver/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>-resources</id>
<phase>package</phase>
<goals>
<goal>-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/maven-archiver/resources</outputDirectory>
<resources>
<resource>
<directory>${basedir}/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<encoding>UTF-8</encoding>
<appendAssemblyId>true</appendAssemblyId>
<descriptors>
<descriptor>${basedir}/src/assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix/>
<mainClass>com.vprisk.main.EtlserverStart</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>bindtothepackagingphase
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>-->
</plugins>
</build>
</project>
Ⅳ 在JAVA的maven項目中怎麼對xml文件進行保存
你加我企鵝
八度重陽在舊山 ,
九九中延九萬年 。
三征不起時賢議 ,
二十三家同願識 。
三春並向指下生 ,
一捧自築珠丘陵 。
七里青灘映碧層 ,
二年辜負兩京春 。
二年疏懶共江潭 ,
Ⅳ java項目中如何用maven獲取資源啊
新建maven類型的項目,然後把這段代碼放在項目的pom.xml文件中就可以了。
Ⅵ JAVA maven創建web項目,把Spring框架配置文件放在src/main/resources中讀取不到配置文件
classpath:是從類路徑里查找配置文件,也就是/WEB-INF/classes目錄下找SpringMVC-servlet.xml。
你寫了classpath了,不會從web-info下找,而是去web-inf/classes下面找,所以找不到。