0

This project is maven project

I want to read a file same package not resource directory

I know that below

  1. read a resource file in resource directory in maven structure
  2. when in runtime, Application read a file in target directory

and really know why directories (source, resource) are splitted.

But I wonder how to read a file in same package not in resource directory even if not formal (just curiosity)

FileIo.java

package hardlearner.springStudy.learningtest.io;

import java.io.IOException;
import java.io.InputStream;

public class FileIo {
    public static void main(String[] args) throws IOException {
        InputStream is = FileIo.class.getResourceAsStream("sample.txt");
        if ( is == null) {
            System.out.println("null");
        }else {
            System.out.println("not null");
            System.out.println((char)is.read());
        }
    }
}

sample.txt

plz read me

FileIo.java's main is working good on normal Java project but maven project isn't

And I checked working good if I copy sample.txt file in same package (io package) in target directory

But not those, how can I read a file on same package in maven project?

read a file from same package

target directory empty resource(sample.txt)

CodeMatrix
  • 2,124
  • 1
  • 18
  • 30
Daivd.DeeplyRooted
  • 27
  • 1
  • 2
  • 18

1 Answers1

0

Give an answer of my question.

First, I don't know clearly how to work maven build about java project.

If I build java project using maven project, files in resource directory is copied to target/classes/

And the other directory files ended '.java' is compiled.

Compiled '.class' files is located to target/classes/{package path}.

But the other files except for ended '.java' is not located to target directory.

Because maven's default build setting copy to target directory only files in resource directory.

So I use custom setting for build.

<build>
    <resources>
        <resource>
            <filtering>true</filtering><!-- if it is neccessary -->
            <directory>${project.basedir}/src/main/java/hardlearner/springStudy/user/sqlservice</directory><!-- from -->
            <targetPath>${project.build.directory}/classes/hardlearner/springStudy/user/sqlservice</targetPath><!-- to -->
            <includes><!-- what -->
                <include>sqlmap.xml</include>
            </includes>
        </resource>
    </resources>
</build>

That setting copy sqlmap.xml in source package to target directory constructed same package structure.

<bean id="sqlService" class="hardlearner.springStudy.user.sqlservice.XmlSqlService">
<!-- value for target/claases/hardlearner/ ... -->    
    <property name="sqlmapFile" value="/hardlearner/springStudy/user/sqlservice/sqlmap.xml"/>
</bean>

if I don't set custom build option for copying file to target directory, sqlService bean's property value is just "/sqlmap.xml". But for that, sqlmap.xml file is located in resource directory.

copy success

Daivd.DeeplyRooted
  • 27
  • 1
  • 2
  • 18