85.字符流和字節流轉換java
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; public class OutputStreamWriterDemo { public static void main(String[] args) { //建立字符流轉換字節流的橋樑的對象 OutputStreamWriter ow=null; try { //字符流通向字節流的橋樑能夠指定存儲的編碼 ow=new OutputStreamWriter(new FileOutputStream("aa.txt"),"GBK"); ow.write("加油"); ow.flush();//操做視做字符流 } catch (Exception e) { e.printStackTrace(); } finally { if(ow!=null){ try { ow.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReaderDemo { public static void main(String[] args) { InputStreamReader ir=null; try { //寫出的時候指定的什麼編碼存儲,讀取的時候指定編碼解碼才能讀取,不然會讀取亂碼 ir=new InputStreamReader(new FileInputStream("aa.txt"),"GBK"); char[] cs=new char[1024]; int len = ir.read(cs); System.out.println(new String(cs,0,len)); } catch (Exception e) { e.printStackTrace(); } finally { if(ir!=null){ try { ir.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
通常開發工具默認是UTF-8編碼安全
86.打印流工具
import java.io.IOException; import java.io.PrintWriter; public class PrintWriterDemo { public static void main(String[] args) { PrintWriter pw=null; try { pw=new PrintWriter("bb.txt"); pw.print("地球人都知道"); pw.print(99); pw.print('a'); pw.print(1.2f); pw.print(new char[]{'s','t','o'}); pw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if(pw!=null){ pw.close(); } } } }
import java.io.*; public class PrintWriterDemo1 { public static void main(String[] args) { PrintWriter pw=null; BufferedReader reader=null; try { pw=new PrintWriter(System.out); reader=new BufferedReader(new FileReader("bb.txt")); //設置一個讀取的標識 String s = null; while ((s=reader.readLine())!=null){ pw.print(s); pw.flush(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if(reader!=null){ reader.close(); } if(pw!=null){ pw.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
87.Properties類開發工具
特色:編碼
1.繼承於Hashtable,是線程安全的鍵值對存儲結構spa
2. Properties 可保存在流中或從流中加載線程
3. 只能保存字符串的鍵值對。3d
import java.util.Properties; public class PropertiesDemo { public static void main(String[] args) { //建立一個Properties Properties properties=new Properties(); //成對存儲字符串 properties.setProperty("001","張二蛋"); properties.setProperty("002","張二瓜"); properties.setProperty("003","張二狗"); properties.setProperty("004","張二餅"); System.out.println(properties); //根據鍵獲取值 String property = properties.getProperty("001"); String property1 = properties.getProperty("002"); String property2 = properties.getProperty("003"); String property3 = properties.getProperty("004"); System.out.println(property); System.out.println(property1); System.out.println(property2); System.out.println(property3); } }
import java.io.PrintWriter; import java.util.Properties; public class PropertiesDemo1 { public static void main(String[] args) { //建立一個Properties Properties properties=new Properties(); //成對存儲字符串 properties.setProperty("001","張二蛋"); properties.setProperty("002","張二瓜"); properties.setProperty("003","張二狗"); properties.setProperty("004","張二餅"); //根據鍵獲取值 String property = properties.getProperty("001"); String property1 = properties.getProperty("002"); String property2 = properties.getProperty("003"); String property3 = properties.getProperty("004"); //建立一個打印流 PrintWriter printWriter=null; try { printWriter=new PrintWriter("cc.properties"); //將properties寫出到printWriter properties.list(printWriter); } catch (Exception e) { e.printStackTrace(); } finally { if(printWriter!=null){ printWriter.close(); } } } }
import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.util.Properties; public class PropertiesDemo2 { public static void main(String[] args) { //建立一個Properties Properties properties=new Properties(); //建立一個打印流 Reader reader=null; try { reader=new FileReader("cc.properties"); //從reader字符流中加載到屬性對象中 properties.load(reader); System.out.println(properties); } catch (Exception e) { e.printStackTrace(); } finally { if(reader!=null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
import java.io.*; import java.util.Properties; public class PropertiesDemo3 { public static void main(String[] args) { //建立一個Properties Properties properties=new Properties(); //建立一個打印流 Reader reader=null; PrintWriter writer=null; OutputStream out=null; try { //建立一個字符輸入流 reader=new FileReader("cc.properties"); //建立一個字符輸出流 writer=new PrintWriter("dd.properties"); //建立一個字節輸出流 out=new FileOutputStream("ee.properties"); //從reader字符流中加載到屬性對象中 properties.load(reader); //再從屬性對象中寫出到writer properties.list(writer); writer.flush(); //將屬性對象寫出到字節輸出流中並添加with store解釋 properties.store(out,"with store"); } catch (Exception e) { e.printStackTrace(); } finally { try { if(reader!=null){ reader.close(); } if(writer!=null){ writer.close(); } if(out!=null){ out.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
實際工做中相對路徑不必定就是src目錄下,要訪問src目錄下文件方法參考以下對象
PropertiesDemo4.class.getClassLoader().getResourceAsStream("ee.properties")blog
import java.io.*; import java.util.Properties; public class PropertiesDemo4 { public static void main(String[] args) { //建立一個Properties Properties properties=new Properties(); InputStream in=null; try { //建立一個字符輸入流,讀取的是與類同目錄下的src中的ee.properties in=PropertiesDemo4.class.getClassLoader().getResourceAsStream("ee.properties"); //從reader字節流中加載到屬性對象中 properties.load(in); System.out.println(properties); } catch (Exception e) { e.printStackTrace(); } finally { try {// if(in!=null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } } }