Java讀取classpath下的文件


寫Java程序時會經常從classpath下讀取文件,是時候該整理一下了,並在不斷深入的過程中,陸續補充上。

現在Java project 都以maven項目居多, 比如像下面這樣的一個項目結構:

編譯后的class文件都到了target目錄,如下面的結構:

看代碼:

import java.io.File;
import java.net.URL;

public class Poem {
    public static void main(String[] args) {

        Poem poem = new Poem();
        poem.getFile("extObj.txt");
    }

    private void getFile(String fileName) {
        ClassLoader classLoader = getClass().getClassLoader();
        /**
        getResource()方法會去classpath下找這個文件,獲取到url resource, 得到這個資源后,調用url.getFile獲取到 文件 的絕對路徑
        */
        URL url = classLoader.getResource(fileName);
        /**
         * url.getFile() 得到這個文件的絕對路徑
         */
        System.out.println(url.getFile());
        File file = new File(url.getFile());
        System.out.println(file.exists());
    }
}

通過上面這種方式就可以獲取到這個文件資源。
在一個static method 里可以直接通過類的ClassLoader對象獲取文件資源。

URL url = Poem.class.getClassLoader().getResource("extObj.txt");
File file = new File(url.getFile());
// 直接獲取到輸入流 
// fileName 就是resources里的文件名  
        InputStream in = Poem.class.getClassLoader().getResourceAsStream(fileName);

綜上述,類里的getClassLoader去尋找fileName都是從classpath去找的,畢竟是ClassLoader嘛。

如果一個包里面有一個配置文件,那該怎么獲取呢? 如圖:

第一個dbconfig.properties在類package下,第二個dbconfig.properties在resources目錄下,
那怎么獲取到package下的dbconfig properties文件呢?
here goes code: 

package com.getfilefromclasspath;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ClassLoaderDemo {
    public static void main(String[] args) throws IOException {
        ClassLoaderDemo demo = new ClassLoaderDemo();
        demo.loadProperties();

    }

    public void loadProperties() throws  IOException {
        InputStream input = null;
        try
        {
            /**
             /dbconfig.properties  絕對路徑, 取到的文件是classpath下的
              resources/dbconfig.properties 相對路徑 獲取文件流
             */
             // 獲取到classpath下的文件
            input = Class.forName(ClassLoaderDemo.class.getName()).getResourceAsStream("/dbconfig.properties");
            // 獲取到package下的文件 
//            input = Class.forName(ClassLoaderDemo.class.getName()).getResourceAsStream("resources/dbconfig.properties");
        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        printProperties(input);
    }

    private void printProperties(InputStream input) throws IOException
    {
        Properties properties = new Properties();
        properties.load(input);
        System.out.println(properties.getProperty("username"));
    }
}

不使用Class.forName(), 通過具體對象獲取到Class對象:

//also can be this way:
input = this.getClass().getResourceAsStream("resources/dbconfig.properties");    // 對應package下的文件 
input = this.getClass().getResourceAsStream("/dbconfig.properties");    // 對應resources下的文件 

Class對象還有getResource() 的方法去獲取文件資源,使用規則和上面的一樣。

maven項目還要注意一點,maven 的compiler插件在編譯時是不會將package下的文本文件給編譯到target下的,
下圖是我在用mybatis框架的時候將xml的mapper給放到package編譯后的效果: 

這個得在pom.xml加對應的配置(這是在使用mybatis時遇到的坑):

<build>
        <finalName>java-io</finalName>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <!--properties的配置文件會和編譯后的class文件放在一起-->
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <!--加載配置的資源-->
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM