我们在用纯Java开发桌面客户端的时候,一版需要把一些配置信息保存下来。这里用到了properties属性。在此我对基本的文件属性操作增删改查封装了一个简单的工具类。使用的时候直接调用即可。。
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
package cn.zxw.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.net.URL; import java.util.Map; import java.util.Properties; /** * @author zxw */ public class PropertiesUtil { private Logger logger = LoggerFactory.getLogger(StringUtil.class); private StringUtil() { } public static String getRootPath(URL url) { String fileUrl = url.getFile(); int pos = fileUrl.indexOf('!'); if (-1 == pos) { return fileUrl; } return fileUrl.substring(5, pos); } public static String trimExtension(String name) { int pos = name.lastIndexOf('.'); if (-1 != pos) { return name.substring(0, pos); } return name; } /** * @param uri * @return */ public static String trimURI(String uri) { String trimmed = uri.substring(1); int splashIndex = trimmed.indexOf('/'); return trimmed.substring(splashIndex); } /** * * @param name * @return * @description 获取相对于resources目录下的路径 */ public static String getFilePathInResources(String name) { String resources = "resources"; int resIdx = name.indexOf(resources); if (resIdx == -1) { return name; } return name.substring(resIdx + resources.length() + 1); } /** * @param name 文件名 * @return * @version 1.2 */ public static String getFileBaseName(String name) { String result = ""; String[] tempStrs = name.split("/"); if (1 == tempStrs.length) { //只有文件名,即name: languageDemo.fxml result = StringUtil.trimExtension(name); } else { result = StringUtil.trimExtension(tempStrs[tempStrs.length - 1]); } return result; } /** * 功能描述:删除文件 <br> * @Param: [fileName] * @Return: boolean * @Author: zxw * @Date: 2020/7/4 20:41 */ public static boolean deleteFile(String fileName) { File file = new File(fileName); // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除 if (file.exists() && file.isFile()) { if (file.delete()) { System.out.println("删除单个文件" + fileName + "成功!"); return true; } else { System.out.println("删除单个文件" + fileName + "失败!"); return false; } } else { System.out.println("删除单个文件失败:" + fileName + "不存在!"); return false; } } //把属性以键值对的形势写入文件 public static void writeFile(String fileName, Properties prop) { try { FileOutputStream oFile = new FileOutputStream(fileName, false); prop.store(oFile, null); oFile.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 获取Properties对象 * @return */ public static Properties getProperties(String fileName){ Properties properties = null; InputStream inputStream = null; try { if (new File(fileName).exists()) { properties = new Properties(); InputStream in = new BufferedInputStream(new FileInputStream(fileName)); properties.load(in); } } catch (FileNotFoundException e) { System.out.println(fileName+"文件未找到!"); } catch (IOException e) { System.out.println("出现IOException"); } finally { try { if (null != inputStream){ inputStream.close(); } } catch (IOException e) { System.out.println(fileName+"文件流关闭出现异常"); } } return properties; } } |
使用案例:
写入—-键值对
1 2 3 4 5 6 7 |
Properties prop = new Properties(); prop.setProperty("userName", userName); prop.setProperty("passwordf", passwordf); prop.setProperty("remember_user", "true"); prop.setProperty("login_auto", "false"); PropertiesUtil.writeFile("login.properties",prop);//调用工具类----写入建值 |
读取—键值对
1 2 3 4 5 6 7 8 9 10 11 |
try { Properties properties = StringUtil.getProperties("login.properties"); if(properties!=null){ String userName = properties.get("userName").toString(); String passwordf = properties.get("passwordf").toString(); String login_auto = properties.get("login_auto").toString();//是否自动登录 String remember_user = properties.get("remember_user").toString();//记住用户名密码 } } catch (Exception e) { e.printStackTrace(); } |