1. Properties 1.概述
2.常用方法
public class PropertiesDemo1 { public static void main(String[] args) { Properties pro = new Properties(); /* setProperty(String key,String value)添加鍵和值 */ pro.setProperty("java01", "001"); pro.setProperty("java02", "002"); /* 通過鍵獲得值:String getProperty(String key) */ System.out.println(pro.getProperty("java01")); /* Set<String>stringPropertyNames()獲得鍵集 */ Set<String> set = pro.stringPropertyNames(); for (String s : set) System.out.println(s + ":" + pro.getProperty(s)); } }結果:001java02:002java01:001
3.設定檔
import java.io.FileReader;import java.io.IOException;import java.util.HashMap;import java.util.Properties;import java.util.Set; public class PropertiesDemo1 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("F:\\pro.txt"));//讀取流 HashMap<String, String> map = new HashMap<String, String>();//map集合 String line = null; while ((line = br.readLine()) != null) { String[] s = line.split("="); map.put(s[0], s[1]); } System.out.println(map); } }結果:{java02=002, java03=003, java01=001}
import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.HashMap;import java.util.Properties;import java.util.Set; public class PropertiesDemo1 { public static void main(String[] args) throws IOException { /* void load(Reader reader)將讀取流字元流載入到集合中 */ Properties pro = new Properties(); FileReader fr = new FileReader("F:\\pro.txt"); pro.load(fr);// 將字元讀取流中讀取的檔案放到Properties對象中 System.out.println("載入後的集合:" + pro); /* 下面我們修改集合中的數值 */ pro.setProperty("java02", "hello"); /* * store(Writer writer,String * comments)通過字元寫入流,把集合中的資訊更新設定檔,comments是注視內容 */ FileWriter fw = new FileWriter("F:\\pro.txt"); pro.store(fw, "java");// 更新設定檔,注釋為:java fr.close(); fw.close(); } }結果:載入後的集合:{java03=003, java02=002, java01=001}
4.計算程式運行次數
importjava.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.PrintStream;import java.util.Properties; public class PropertiesDemo { publicstatic void main(String[] args) throws IOException { /* * 計算程式的運行次數 思路: 1.建立一個設定檔,然後然后里面定義了一個計數器 * 2.每次啟動並執行時候,都取出來,然後自增,然後再存回去 */ Filefile = new File("F:\\pro.ini"); if(!file.exists()) file.createNewFile(); Propertiespro = new Properties(); pro.load(newFileInputStream(file));// 載入流 Stringvalue = pro.getProperty("count"); intcount = 0; if(value != null) count= Integer.parseInt(value); count++;//自增 pro.setProperty("count",count + "");// 改變值 pro.store(newFileOutputStream(file), "java for count");// 更新檔案 System.out.println(pro); } }結果:運行一次,count就+1.
2. 列印流
import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter; public class PrintStreamDemo { public static void main(String[] agrs) throws IOException { // File f=newFile("F:\\demo.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out,true);//true表示自動重新整理,但是必須是println方法 String line = null; while ((line = br.readLine()) != null) { if ("over".equals(line)) return; pw.println("內容是:" + line); //pw.flush(); } br.close(); pw.close(); }}
3. 合并流
SequenceInputStreamEnumerationInputStream
import java.io.FileOutputStream;import java.io.IOException;import java.io.SequenceInputStream;import java.util.Enumeration;import java.util.Vector;import java.io.FileInputStream; public class SqueDemo { public static void main(String[] agrs) throws IOException { Vector<FileInputStream> v = new Vector<FileInputStream>(); v.add(new FileInputStream("F:\\1.txt")); v.add(new FileInputStream("F:\\2.txt")); v.add(new FileInputStream("F:\\3.txt")); Enumeration<FileInputStream> en = v.elements(); SequenceInputStreamsis = new SequenceInputStream(en); FileOutputStream out = new FileOutputStream("F:\\4.txt"); byte[] b = new byte[1024]; int len = 0; while ((len = sis.read(b)) != -1) { out.write(b, 0, len); out.flush(); } sis.close(); out.close(); }}
4. 切割流
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.SequenceInputStream;import java.util.Enumeration;import java.util.Vector; public class PhotoDemo { public static void main(String[] args) throws IOException { FenGe(); HeBing(); } private static void HeBing() throws FileNotFoundException, IOException { /* 其實這個也可以使用迴圈,然後添加到集合, */ Vector<FileInputStream> v = new Vector<FileInputStream>(); v.add(new FileInputStream("F:\\part\\1.part")); v.add(new FileInputStream("F:\\part\\2.part")); v.add(new FileInputStream("F:\\part\\3.part")); v.add(new FileInputStream("F:\\part\\4.part")); v.add(new FileInputStream("F:\\part\\5.part")); Enumeration<FileInputStream> en = v.elements(); SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream out = new FileOutputStream("F:\\part\\1.bmp"); byte[] b = new byte[1024]; int len = 0; while ((len = sis.read(b)) != -1) { out.write(b, 0, len); out.flush(); } sis.close(); out.close(); } /* 分割 */ private static void FenGe() throws FileNotFoundException, IOException { FileInputStream input = new FileInputStream("F:\\1.png"); FileOutputStream out = null; byte[] buf = new byte[1024 * 100]; int count = 1; int len = 0; while ((len = input.read(buf)) != -1) { out = new FileOutputStream("F:\\part\\" + (count++) + ".part");// 分割後的尾碼名自己可以自訂 out.write(buf, 0, len); out.flush(); out.close(); } input.close(); } }