讀取properties檔案方法,properties檔案無法刪除
阿新 • • 發佈:2019-01-13
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.Properties; public class readProperties { /** * @param args */ public static void main(String[] args) { try { Properties pro =new Properties(); pro.load(new InputStreamReader(Object.class.getResourceAsStream("/1.properties"), "UTF-8")); System.out.println("讀取專案中檔案:"+pro.getProperty("name"));
Properties properties = new Properties(); properties.load(new InputStreamReader(new BufferedInputStream(new FileInputStream("D:/Develop/2.properties")),"UTF8")); System.out.println("讀取本地檔案:"+properties.getProperty("name")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
properties檔案是以鍵值對的形式存放的
如果出現中文亂碼的問題,那麼就得加上字符集、
如果後面你還要對properties檔案進行其他操作,比如刪除file.delete(),那麼就得關閉io,解除對properties檔案的呼叫,不然這個檔案會刪除失敗
FileInputStream fIn = new FileInputStream("D:/Develop/2.properties"); Properties properties = new Properties(); properties.load(new InputStreamReader(new BufferedInputStream(fIn), "utf-8")); fIn.close();//將流關閉