package com.zkn.newlearn.others; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import com.zkn.newlearn.gof.singleton.SimpleFactoryTest01; /** * Five ways to read resource files * @author zkn */ public class ClassReadResourceDemo { public static void main(String[] args) { /** * The first way uses the class loader to read the resource file. * Applicable situation: Resource files and class files can be in different directories. * Note: The parameters in getResourceAsStream must be * Fully qualified path to write resource file, package name + file name * Do not write "/" at the beginning */ InputStream is = ClassReadResourceDemo.class. getClassLoader().getResourceAsStream("com/zkn/newlearn/io/config.properties"); Properties prop = new Properties(); try { prop.load(is); System.out.println(prop.getProperty("key")); is.close(); } catch (IOException e) { e.printStackTrace (); } /** * The second way of writing: use class.getResourceAsStream() (in fact, the class loader is still used) * Applicable situation: If the resource file and the class file are in the same package, just write the name of the resource file directly. * Note: There is no need to add "/" before the name of the resource file */ is = ClassReadResourceDemo.class.getResourceAsStream("config.properties"); try { prop.load(is); System.out.println(prop.getProperty("key")); is.close(); } catch (IOException e) { e.printStackTrace (); } /** * The third way of writing: use class.getResourceAsStream() (in fact, the class loader is still used) * Applicable situation: This writing method is applicable when the resource file and the class file are not in the same directory * Note: Be sure to add "/" at the beginning */ is = ClassReadResourceDemo.class.getResourceAsStream("/com/zkn/newlearn/io/config.properties"); try { prop.load(is); System.out.println(prop.getProperty("key")); is.close(); } catch (IOException e) { e.printStackTrace (); } /** * The fourth way of writing: use class.getResourceAsStream() * Applicable situation: This writing method is suitable for the case where the resource file is in the root directory * Note: Be sure to add "/" before the file name */ is = ClassReadResourceDemo.class.getResourceAsStream("/config.properties"); try { prop.load(is); System.out.println(prop.getProperty("key")); is.close(); } catch (IOException e) { e.printStackTrace (); } /** * The fifth way of writing: use the class loader to read the resource file * Applicable situation: The resource file is in the following directory * Note: Do not add "/" before the resource file name */ is = ClassReadResourceDemo.class.getClassLoader().getResourceAsStream("config.properties"); try { prop.load(is); System.out.println(prop.getProperty("key")); is.close(); } catch (IOException e) { e.printStackTrace (); } } }
5 ways to read resource files in Java
https://www.cnblogs.com/EasonJim/p/6517653.html#autoid-0-0-0
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326943348&siteId=291194637
Recommended
Ranking