摘要

尝试读取三个不同位置的文件:

文件位置 读取方法
同一个 package 下 App.class.getResourceAsStream("db1.properties")
resources 目录下 App.class.getClassLoader().getResourceAsStream("db2.properties")
项目根目录下 new FileInputStream("xanadu.txt")

项目截图

读取文件中的路径问题

目录结构

learn-java/
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── feng
│   │   │           └── learnjava
│   │   │               ├── App.java
│   │   │               └── db1.properties ????
│   │   └── resources
│   │       └── db2.properties ????
│   └── test
│       └── java
├── target
│   ├── classes
│   │   ├── com
│   │   │   └── feng
│   │   │       └── learnjava
│   │   │           ├── App.class
│   │   │           └── db1.properties ???? # App.class.getResourceAsStream("db1.properties")
│   │   ├── db2.properties ???? # App.class.getClassLoader().getResourceAsStream("db2.properties")
│   │   ├── META-INF
│   │   │   ├── MANIFEST.MF
│   │   │   └── maven
│   │   │       └── com.feng
│   │   │           └── learn-java
│   │   │               ├── pom.properties
│   │   │               └── pom.xml
│   │   └── properties
│   └── test-classes
└── xanadu.txt ???? # new FileInputStream("xanadu.txt")

InputStream

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

public class App {
    public void getInputStream() {

        //------ db1.properties ------//
        try (InputStream in = App.class.getResourceAsStream("db1.properties")) {
            Properties properties = new Properties();
            try {
                properties.load(in);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String username = properties.getProperty("username");
            System.out.println(username);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //------ db2.properties ------//
        try (InputStream in = App.class.getClassLoader().getResourceAsStream("db2.properties")) {
            Properties properties = new Properties();
            try {
                properties.load(in);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String username = properties.getProperty("username");
            System.out.println(username);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //------ xanadu.txt ------//
        try (InputStream in = new FileInputStream("xanadu.txt")) {
            // do something
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

FileInputStream

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;

public class App {
    public static void getFileInputStream() {

        //------ db1.properties ------//
        URL url1 = App.class.getResource("");
        System.out.println(url1.toString());
        //file:/D:/fengxinbao/workspace/learn-java/target/classes/com/feng/leanjava/
        String path1 = url1.toString().replace("file:/", "");
        try (FileInputStream in = new FileInputStream(path1 + "db1.properties")) {
            // do something
        } catch (IOException e) {
            e.printStackTrace();
        }

        //------ db2.properties ------//
        URL url2 = App.class.getClassLoader().getResource("");
        System.out.println(url2.toString());
        //file:/D:/fengxinbao/workspace/learn-java/target/classes/
        String path2 = url2.toString().replace("file:/", "");
        try (FileInputStream in = new FileInputStream(path2 + "db2.properties")) {
            // do something
        } catch (IOException e) {
            e.printStackTrace();
        }

        //------ xanadu.txt ------//
        try (FileInputStream in = new FileInputStream("xanadu.txt")) {
            // do something
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
}

相关文章: