Properties 文件使用默认字符编码:ISO-8859-1,请勿修改。
package com.chenwc; import java.io.*; import java.nio.file.Files; import java.util.Properties; public class PropertiesFileUtil { /** * 通过文件路径读取 Properties 文件 * @param path 文件路径 * @return Properties */ public static Properties readPropertiesFile(String path) { Properties properties = new Properties(); File file = new File(path); try { properties = readPropertiesFile(Files.newInputStream(file.toPath())); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return properties; } /** * 通过流读取 Properties 文件 * * @param is InputStream * @return Properties */ public static Properties readPropertiesFile(InputStream is) { Properties properties = new Properties(); try { properties.load(is); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { try { if (null != is) { is.close(); } } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } return properties; } /** * 写 Properties 文件(不追加文件内容) * @param properties Properties * @param path 写入文件路径 */ public static void writePropertiesFile(Properties properties, String path){ writePropertiesFile(properties, path, "", false); } /** * 写 Properties 文件(不追加文件内容),写入时加入描述 comments * @param properties Properties * @param comments 描述 * @param path 写入文件路径 */ public static void writePropertiesFile(Properties properties, String path, String comments){ writePropertiesFile(properties, path, comments, false); } /** * 写 Properties 文件 * @param properties Properties * @param path 写入文件路径 * @param comments 描述 * @param isAppend 是否把数据追加到文件 */ public static void writePropertiesFile(Properties properties, String path, String comments, boolean isAppend){ File file = new File(path); mkdirParents(file); FileOutputStream fos = null; try { if (!file.exists()) { if (!file.createNewFile()){ System.out.println("文件不存在,创建文件失败!"); return; } else { System.out.println("文件不存在,创建文件成功!"); } } fos = new FileOutputStream(file, isAppend); properties.store(fos, comments); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } finally { try { if (null != fos) { fos.close(); } } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } } /** * 判断文件的父级目录是否存在,不存在则创建 * @param file 文件 * @return true 父级目录存在或创建父级目录成功, false创建父级目录失败 */ private static boolean mkdirParents(File file){ if (!file.getParentFile().exists()) { return file.getParentFile().mkdirs(); } else { return true; } } }
package com.chenwc; import java.io.File; import java.io.InputStream; import java.util.Map.Entry; import java.util.Properties; import java.util.UUID; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 File file = new File("1.txt"); //当前项目根目录 String path = file.getAbsolutePath().replace("1.txt", ""); System.out.println("----------------通过文件绝对路径读取-----------------"); String pathRead = path + "src\\main\\resources\\log4j.properties"; Properties properties1 = PropertiesFileUtil.readPropertiesFile(pathRead); for (Entry<Object, Object> entry : properties1.entrySet()) { String key = (String) entry.getKey(); String value = (String) entry.getValue(); System.out.println("key: " + key + "\tvalue:" + value); } System.out.println("\r\n--------------------通过流读取---------------------"); InputStream is = Main.class.getClassLoader().getResourceAsStream("log4j.properties"); Properties properties2 = PropertiesFileUtil.readPropertiesFile(is); for (Entry<Object, Object> entry : properties2.entrySet()) { String key = (String) entry.getKey(); String value = (String) entry.getValue(); System.out.println("key: " + key + "\tvalue:" + value); } Properties properties3 = new Properties(); properties3.setProperty("131231", "e1rthref"); properties3.setProperty("yingwen", "dwdqwq"); properties3.setProperty("中文", "中文格尔无任何特王企鹅问问热"); properties3.setProperty("3121", String.valueOf(System.currentTimeMillis())); properties3.setProperty("UUID", UUID.randomUUID().toString()); PropertiesFileUtil.writePropertiesFile(properties3, path + "log4j.properties", "test write", true); } }