Properties(配置文件類): 主要用於生產配置文件與讀取配置文件的信息。java
Properties要注意的細節:
1. 若是配置文件的信息一旦使用了中文,那麼在使用store方法生成配置文件的時候只能使用字符流解決,若是使用字節流生成配置文件的話,
默認使用的是iso8859-1碼錶進行編碼存儲,這時候會出現亂碼。
2. 若是Properties中的內容發生了變化,必定要從新使用Properties生成配置文件,不然配置文件信息不會發生變化。
數組
import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; //存配置文件 public class Demo1 { public static void main(String[] args) throws IOException { Properties p = new Properties(); p.setProperty("李傑", "999"); p.setProperty("李英", "666"); p.setProperty("李漢三", "888"); /*複習遍歷(properties基於hashtable基於Map,因此用entrySet遍歷) Set<Entry<Object, Object>> a = p.entrySet(); for(Entry<Object, Object> b:a) { System.out.println(b); }*/ //使用Properties產生配置文件 //方法一:使用字節輸出流沒法輸出漢字,只會出現亂碼,由於底層用的是拉丁字符表 p.store(new FileOutputStream("F:\\a.properties"), "way:1"); //方法二:使用字符輸出流輸出漢字,底層用的是GBK表 p.store(new FileWriter("F:\\b.properties"), "way:2");//way:2處仍是沒法正常解碼中文,須要注意 } }
//加載、修改配置文件 import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; public class Demo2 { public static void main(String[] args) throws IOException { readProperties(); } public static void readProperties() throws IOException{ Properties properties = new Properties(); properties.load(new FileReader("F:\\b.properties")); //遍歷properties判斷是否加載成功 Set<Entry<Object, Object>> entrys = properties.entrySet(); for(Entry<Object, Object> entry :entrys){ System.out.println("鍵:"+ entry.getKey() +" 值:"+ entry.getValue()); } //修改李傑的密碼,把修改後的Properties再生成一個配置文件 properties.setProperty("李傑", "007"); properties.store(new FileWriter("F:\\b.properties"), "ways:2"); } }
方法一效果jvm
#way:1
#Sat Aug 04 11:08:30 CST 2018
\u674E\u6770=999
\u674E\u6C49\u4E09=888
\u674E\u82F1=666ide
方法二效果this
#ways:2
#Sat Aug 04 11:08:37 CST 2018
李傑=007
李漢三=888
李英=666編碼
練習: 使用properties實現本軟件只能 運行三次,超過了三次以後就提示購買正版,退jvm.spa
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class Demo3 { public static void main(String[] args) throws IOException { File file = new File("F:\\count.properties"); if(!file.exists()){ file.createNewFile();//若是配置文件不存在,則建立該配置文件 } Properties properties = new Properties(); properties.load(new FileInputStream(file));//把配置文件的信息加載到properties中 FileOutputStream fileOutputStream = new FileOutputStream(file);//必須放在加載以後,否則他會清空文件內容 int count = 0; //定義該變量是用於保存軟件的運行次數的。 String value = properties.getProperty("count");//讀取配置文件的運行次數 if(value!=null){ count = Integer.parseInt(value); } //判斷使用的次數是否已經達到了三次, if(count==3){ System.out.println("你已經超出了試用次數,請購買正版軟件!!"); System.exit(0); } count++; System.out.println("你已經使用了本軟件第"+count+"次"); properties.setProperty("count",count+""); //使用Properties生成一個配置文件 properties.store(fileOutputStream,"runtime"); } }
打印流(printStream) 打印流能夠打印任意類型的數據,並且打印數據以前都會先把數據轉換成字符串再進行打印。
code
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; class Animal{ String name; String color; public Animal(String name,String color){ this.name = name; this.color = color; } @Override public String toString() { return "名字:"+this.name+ " 顏色:"+ this.color; } } public class Demo4 { public static void main(String[] args) throws IOException { /*FileOutputStream fileOutputStream = new FileOutputStream("F:\\a.txt"); fileOutputStream.write("97".getBytes()); fileOutputStream.close();*/ File file = new File("F:\\a.txt"); //建立一個打印流 PrintStream printStream = new PrintStream(file); /* printStream.println(97); printStream.println(3.14); printStream.println('a'); printStream.println(true); Animal a = new Animal("天空", "藍色"); printStream.println(a); //默認標準的輸出流就是向控制檯輸出的, System.setOut(printStream); //從新設置了標準的輸出流對象 System.out.println("的健身卡好的艱苦"); */ File logFile = new File("F:\\2015年1月8日.log"); PrintStream logPrintStream = new PrintStream( new FileOutputStream(logFile,true) ); try{ int c = 4/0; System.out.println("c="+c); int[] arr = null; System.out.println(arr.length); }catch(Exception e){ e.printStackTrace(logPrintStream); } } }
編碼與解碼
編碼: 把看得懂的字符變成看不懂碼值這個過程咱們稱做爲編碼。
解碼: 把碼值查找對應的字符,咱們把這個過程稱做爲解碼。對象
注意: 之後編碼與解碼通常咱們都使用統一的碼錶。不然很是容易出亂碼。blog
import java.io.UnsupportedEncodingException; import java.util.Arrays; public class Demo7 { public static void main(String[] args) throws Exception { /* String str = "中國"; byte[] buf = str.getBytes("utf-8");// 平臺默認的編碼表是gbk編碼表。 編碼 System.out.println("數組的元素:"+Arrays.toString(buf)); // str = new String(buf,"utf-8"); //默認使用了gbk碼錶去解碼。 System.out.println("解碼後的字符串:"+ str); */ /*String str = "a中國"; // ,0, 97, 78, 45, 86, -3 byte[] buf = str.getBytes("unicode"); //編碼與解碼的時候指定 的碼錶是unicode實際上就是用了utf-16. System.out.println("數組的內容:"+ Arrays.toString(buf)); */ String str = "你們好"; byte[] buf = str.getBytes(); //使用gbk進行編碼 System.out.println("字節數組:"+ Arrays.toString(buf)); // -76, -13, -68, -46, -70, -61 str = new String(buf,"iso8859-1"); // 出現亂碼以後均可以被還原嗎? byte[] buf2 = str.getBytes("iso8859-1"); str = new String(buf2,"gbk"); System.out.println(str); } }