ファイル絶対パス取得の問題

通常、  FileReader reader = new FileReader( "classToGet.properties"); Get pathの問題ですが、このパス方法の欠陥:移植性が低く、アイデアを離れて別の場所に変更すると、パスが無効になります。

1.より一般的なパス取得

ただし、前提がありますこのファイルはクラスパスに含まれている必要があります(つまりsrcの下にあるものはすべてクラスパスです)。

コードは次のように表示されます。

public class reflect_path {
    public static void main(String[] args) throws FileNotFoundException {
        //这个是在src的bean目录下的
        String path = Thread.currentThread().getContextClassLoader().getResource("cn/com/Ycy/Beans/name.properties").getPath();
        System.out.println(path);
        String path2 = Thread.currentThread().getContextClassLoader().getResource("classToGet.properties").getPath();
        System.out.println(path2);
    }
}

出力結果:

/D:/ideaProject/TCP/InetAddress/out/production/InetAddress/cn/com/Ycy/Beans/name.properties
/D:/ideaProject/TCP/InetAddress/out/production/InetAddress/classToGet.properties

したがって、作成オブジェクトは次のように記述できます。

public class test03 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        String path = Thread.currentThread().getContextClassLoader().getResource("classToGet.properties").getPath();
        FileReader reader = new FileReader(path);
        //创建属性类对象map
        Properties pro = new Properties();
        //加载
        pro.load(reader);
        //关闭流
        reader.close();
        String className = pro.getProperty("className");
        Class c = Class.forName(className);
        Object user = c.newInstance();
        System.out.println(user);
    }
}

2.ストリームの形で直接戻る

xxx.propertiesファイルを取得するためのパスを取得してストリーム形式に直接戻る代わりに、コードの簡略化された形式にすることができます。FileReaderを使用する必要はありません。

public class IO_return {
    public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        InputStream reader = Thread.currentThread().getContextClassLoader().getResourceAsStream("classToGet.properties");
        //创建属性类对象map
        Properties properties = new Properties();
        //加载
        properties.load(reader);
        reader.close();
        Class c = Class.forName(properties.getProperty("className"));
        Object obj = c.newInstance();
        System.out.println(obj);
    }
}

3.リソースバインダー(最も簡単な方法)

はい、前は雪が降っています、ははは、だまされたような気がします。とても便利です。

java.util.ResourceBundle
       クラスは、プロパティ構成ファイルのコンテンツを取得するために特に使用されます

「xxx.properties」プロパティ構成ファイルのみを読み取ることができ、ファイルはsrcクラスパスに配置する必要があります

注:このクラスは「xxx.properties」プロパティ構成ファイルのみを読み取ることができ、ファイルはsrcクラスパスに配置する必要があります。

コードデモ:

public class ResourceBundleTest {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        //这个name.properties是在Beans目录下的
        ResourceBundle bundle = ResourceBundle.getBundle("cn/com/Ycy/Beans/name");
        String className = bundle.getString("className");
        Class c  = Class.forName(className);
        Object obj = c.newInstance();
        System.out.println(obj);


        ResourceBundle bundle2 = ResourceBundle.getBundle("classToGet");
        String className2 = bundle2.getString("className");
        Class c2  = Class.forName(className2);
        Object obj2 = c2.newInstance();
        System.out.println(obj2);
    }
}

おすすめ

転載: blog.csdn.net/weixin_43725517/article/details/108300774