日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux properties 出现java.io.FileNotFoundException

發(fā)布時(shí)間:2024/1/23 linux 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux properties 出现java.io.FileNotFoundException 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原因?yàn)?#xff1a;之前寫的properties工具類只支持Windows平臺(tái),以下是兼容 windows和linux兩個(gè)平臺(tái)的 properties工具類


package com.cpcnet.util;import java.io.*; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties;public class PropertiesUtil {/** * 得到某一個(gè)類的路徑 * * @param name * @return */ public static String getPath(Class name) { String strResult = null; if (System.getProperty("os.name").toLowerCase().indexOf("window") > -1) { strResult = name.getResource("/").toString().replace("file:/", "") .replace("%20", " "); } else { strResult = name.getResource("/").toString().replace("file:", "") .replace("%20", " "); } return strResult; } /** * 讀取所有的property * @param filename properties文件路徑 * @return 所有的property的集合(map形式) */ @SuppressWarnings("unchecked") public static Map<String, String> getPropties(String filename) { if (null == filename) { return null; } String filePath = getPath(PropertiesUtil.class) + filename; Properties props = new Properties(); InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(filePath)); props.load(in); Map<String, String> map = new HashMap<String, String>(); Enumeration en = props.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String Property = props.getProperty(key); map.put(key, Property); } return map; // 關(guān)閉資源 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } /** * 獲取某個(gè)property的值 * @param filename 文件名 * @param key property的key * @return property的value */ public static String getProp(String filename, String key) { if (null == filename || null == key) { return null; } String filePath = getPath(PropertiesUtil.class) + filename; Properties props = new Properties(); InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(filePath)); props.load(in); return props.getProperty(key); // 關(guān)閉資源 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } }



總結(jié)

以上是生活随笔為你收集整理的linux properties 出现java.io.FileNotFoundException的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。