使用Spring读取package里面的Proerites文件
1 2 |
Java代码 Properties detailProps = loadProperties("com/test.properties"); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Java代码 protected Properties loadProperties(String resource) throws IOException { Properties props = new Properties(); InputStream is = null; try { is = ResourceHelper.getClassPathResourceAsInputStream(resource); if (is != null) { propertiesPersister.load(props, new InputStreamReader(is, "UTF-8")); } } finally { IOUtils.closeQuietly(is); } return props; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
Java代码 package com.tools; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.util.ResourceUtils; /** * 资源操作工具类 * * @author ourenyang * */ public class ResourceHelper { /** * 在指定类所在包下查找指定的资源文件,并转换成InputStream * * @param file * @param clazz * @return * @throws IOException */ public static InputStream getResourceInPackage(String file, Class<?> clazz) throws IOException { StringBuilder path = new StringBuilder(clazz.getPackage().getName().replace('.', '/')); if (file.charAt(0) != '/') { path.append('/'); } path.append(file); Resource res = new ClassPathResource(path.toString()); return res.getInputStream(); } /** * 在指定类所在包下查找指定的资源文件,并转换成InputStream * * @param file * @param clazz * @return * @throws IOException */ public static Reader getResourceInPackageAsReader(String file, Class<?> clazz) throws IOException { StringBuilder path = new StringBuilder(clazz.getPackage().getName().replace('.', '/')); if (file.charAt(0) != '/') { path.append('/'); } path.append(file); Resource res = new ClassPathResource(path.toString()); return new InputStreamReader(res.getInputStream()); } /** * 从Class路径中获取指定资源文件,并转换成InputStream * * @param path * @return * @throws IOException */ public static InputStream getClassPathResourceAsInputStream(String path) throws IOException { Resource res = new ClassPathResource(path); return res.getInputStream(); } /** * 获取指定资源文件,并转换成InputStream,资源文件名规则见Spring * * @param path * @return * @throws IOException /** * 从Class路径中获取指定资源文件,并转换成Reader * * @param path * @return * @throws IOException */ public static Reader getClassPathResourceAsReader(String path) throws IOException { Resource res = new ClassPathResource(path); InputStream is = res.getInputStream(); return new InputStreamReader(is); } /** * 获取指定资源文件,并转换成Reader,资源文件名规则见Spring * * @param path * @return * @throws IOException */ public static Reader getResourceAsReader(String path) throws IOException { File file = ResourceUtils.getFile(path); return new FileReader(file); } } */ public static InputStream getResourceAsInputStream(String path) throws IOException { File file = ResourceUtils.getFile(path); return new FileInputStream(file); } /** * 从Class路径中获取指定资源文件,并转换成Reader * * @param path * @return * @throws IOException */ public static Reader getClassPathResourceAsReader(String path) throws IOException { Resource res = new ClassPathResource(path); InputStream is = res.getInputStream(); return new InputStreamReader(is); } /** * 获取指定资源文件,并转换成Reader,资源文件名规则见Spring * * @param path * @return * @throws IOException */ public static Reader getResourceAsReader(String path) throws IOException { File file = ResourceUtils.getFile(path); return new FileReader(file); } } |