maven 打JAR包资源文件指定路径与文件读取

时间:2022-07-03 09:15:34

       

     1.配置RESOURCES节点

 有时会遇到maven打包后找不到资源文件了,其实这时候路径发生了变化,为了确保打包后路径不发生变化,指定一个固定的路径,请看下面配置


<build>
  ...
  <resources>
    <resource>
      <filtering>true</filtering>
      <directory>src/main/command</directory>
      <includes>
        <include>run.bat</include>
        <include>run.sh</include>
      </includes>
      <targetPath>/abc</targetPath>
    </resource>
    <resource>
      <directory>src/main/scripts</directory>
    </resource>
  </resources>
  ...
</build>


2.读取文件:

			
public static String path="resources/datatype.txt";
//String ss= App.class.getClassLoader().getResource("resources/datatype.txt").getPath();
			InputStream in = App.class.getClassLoader().getResourceAsStream(  
	                path);// 读取流文件
			//File csv = new File(ss); // CSV文件
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            // 读取直到最后一行 
            String line = ""; 
            //br.readLine();//跳过表头
            while ((line = br.readLine()) != null) { 
                // 把一行数据分割成多个字段 
                //StringTokenizer st = new StringTokenizer(line, ",");
                String[] lin=App.DELIMITER.split(line.toString(),2);
                map.put(lin[0],lin[1]);
                
                System.out.println(line); 
            } 
            br.close();


版权声明:本文为博主原创文章,未经博主允许不得转载。