maven项目读取文件|java项目中如何用maven获取资源啊

Ⅰ 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路径在弄清楚编译后,资源文件以及字节码存在哪里这个问题之前,有必要明白什么是classpathclasspath实际上就是编译后的 以 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/hahahahahahahahahhahahahahahahahahahhahahahahahahahahahha从上面可以发现getResource 与 class.getClassLoader().getResource两者的区别:前者获取的是当前类加载的路径,如果用此方法读取文件则有两种方法,与相对路径绝对路径非常类似,具体参见代码后者获取的是类加载器的路径,即会到classpath路径下。可以理解当前在 classp/ 目录下,要想访问哪个文件,直接填写路径即可,不用区分相对路径和绝对路径。显然,此方法比较容易写出。推荐。.gif相关Related PostsJAVA: 理解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"?><projectxsi: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下面找,所以找不到。


赞 (0)