一署鸡、標準輸入流 & 轉(zhuǎn)換流 & 打印流
public static final InputStream in:標準輸入流
public static final PrintStream out:標準輸出流package com.neuedu.demo; /* * 標準輸入輸出流: * public static final InputStream in:字節(jié)輸入流,用來讀取鍵盤錄入的數(shù)據(jù) * public static final int x; * InputStream is = System.in; * Scanner sc = new Scanner(System.in); * public static final PrintStream out:字節(jié)輸出流,將數(shù)據(jù)輸出到命令行 * System.out.println(); */ public class SystemInOutDemo { }
- 轉(zhuǎn)換流
- OutputStreamWriter:將字節(jié)輸出流轉(zhuǎn)換為字符輸出流
- 案例代碼一:
package com.neuedu.demo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; /* * 需求:讀取項目根目錄下的SystemInOutDemo.java毕箍,并輸出到命令行 * * 數(shù)據(jù)源:項目根目錄下的SystemInOutDemo.java BufferedReader * 目的地:命令行 System.out * * * 由于標準輸出流是一個字節(jié)輸出流靶累,所以只能輸出字節(jié)或者字節(jié)數(shù)組,但是我們讀取到的數(shù)據(jù)則是字符串枢里,如果想進行輸出還需要轉(zhuǎn)換成字節(jié)數(shù)組 * 我們要想通過標準輸出流輸出字符串探越,把標準輸出流轉(zhuǎn)換成一種字符輸出流即可,OutputStreamWriter * * OutputStreamWriter(OutputStream out) :轉(zhuǎn)換流腐螟,把字節(jié)輸出流轉(zhuǎn)換成字符輸出流 * * */ public class OutputStreamWriterDemo { public static void main(String[] args) throws IOException { //method2(); //創(chuàng)建輸入流對象 BufferedReader br = new BufferedReader(new FileReader("SystemInOutDemo.java")); //創(chuàng)建輸出流對象 //OutputStream os = System.out; //Writer w = new OutputStreamWriter(System.out);//多態(tài)贤牛,父類型引用指向子類對象 //BufferedWriter bw = new BufferedWriter(w); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); //進行數(shù)據(jù)的讀寫 String line;//用于存儲讀取到的數(shù)據(jù) while((line = br.readLine()) != null) { bw.write(line); bw.newLine(); } //釋放資源 bw.close(); br.close(); } private static void method2() throws FileNotFoundException, IOException { //創(chuàng)建輸入流對象 BufferedReader br = new BufferedReader(new FileReader("SystemInOutDemo.java")); //創(chuàng)建輸出流對象 //OutputStream os = System.out; Writer w = new OutputStreamWriter(System.out);//多態(tài),父類型引用指向子類對象 //進行數(shù)據(jù)的讀寫 String line;//用于存儲讀取到的數(shù)據(jù) while((line = br.readLine()) != null) { w.write(line); w.write("\r\n"); } //釋放資源 w.close(); br.close(); } private static void method() throws FileNotFoundException, IOException { //創(chuàng)建輸入流對象 BufferedReader br = new BufferedReader(new FileReader("SystemInOutDemo.java")); //創(chuàng)建輸出流對象 OutputStream os = System.out; String line;//用于存儲讀取到的數(shù)據(jù) while((line = br.readLine()) != null) { os.write(line.getBytes()); os.write("\r\n".getBytes()); } //釋放資源 os.close(); br.close(); } }
- InputStreamReader:將字節(jié)輸入流轉(zhuǎn)換為字符輸入流
- 案例代碼二:
package com.neuedu.demo; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; /* * 需求:讀取鍵盤錄入的數(shù)據(jù)杨耙,并輸出到項目根目錄下的a.txt文件中 * * 數(shù)據(jù)源:讀取鍵盤錄入的數(shù)據(jù) System.in * 目的地:項目根目錄下的a.txt FileWriter * * * * 轉(zhuǎn)換流:需要把字節(jié)輸入流轉(zhuǎn)換成字符輸入流鲤氢,InputStreamReader * InputStreamReader(InputStream in) */ public class InputStreamReaderDemo { public static void main(String[] args) throws IOException { //創(chuàng)建輸入流對象 InputStream is = System.in; Reader r = new InputStreamReader(is); //創(chuàng)建輸出流對象 FileWriter fw = new FileWriter("a.txt"); //讀寫數(shù)據(jù) char[] chs = new char[1024]; int len; while((len = r.read(chs)) != -1) { fw.write(chs,0,len); fw.flush(); } //釋放資源 fw.close(); is.close(); } private static void method() throws IOException { //創(chuàng)建輸入流對象 InputStream is = System.in; //創(chuàng)建輸出流對象 FileWriter fw = new FileWriter("a.txt"); //讀寫數(shù)據(jù) byte[] bys = new byte[1024]; int len;//用于存儲讀取到的字節(jié)個數(shù) while((len = is.read(bys)) != -1) { fw.write(new String(bys,0,len)); fw.flush(); } //釋放資源 fw.close(); is.close(); } }
- 打印流
- 打印流添加輸出數(shù)據(jù)的功能,使它們能夠方便地打印各種數(shù)據(jù)值表示形式.
字符打印流 PrintWriter
void print(String str): 輸出任意類型的數(shù)據(jù)汽绢,
void println(String str): 輸出任意類型的數(shù)據(jù)吗跋,自動寫入換行操作
- 案例代碼三:
package com.neuedu.demo; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; /* * 打印流: * PrintStream * PrintWriter * 可以自動換行,println() * 不能輸出字節(jié),但是可以輸出其他任意類型 * 通過某些配置跌宛,可以實現(xiàn)自動刷新(只有在調(diào)用 println酗宋、printf 或 format才有用) * 也是包裝流,不具備寫出功能 * 可以把字節(jié)輸出流轉(zhuǎn)換成字符輸出流 * * 注意:只能輸出不能輸入 * * */ public class PrintWriterDemo { public static void main(String[] args) throws IOException { //創(chuàng)建打印流對象 PrintWriter pw = new PrintWriter("b.txt"); //寫出數(shù)據(jù) pw.write("hello"); pw.write("world"); pw.write("java"); //釋放資源 pw.close(); } }
- 案例代碼四:
利用打印流實現(xiàn)自動換行與自動更新package com.neuedu.demo; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; /* * 打印流的特有功能: * 自動換行 使用方法println()實現(xiàn)自動換行 * 自動刷新 創(chuàng)建PrintWriter對象時啟動自動刷新開關(guān)疆拘,并且使用println等3個方法可以實現(xiàn)自動刷新 * * 注意:創(chuàng)建FileWriter對象時boolean參數(shù)是是否追加蜕猫, * 而創(chuàng)建打印流對象的boolean類型參數(shù)是是否自動刷新 */ public class PrintWriterDemo2 { public static void main(String[] args) throws IOException { //method(); //創(chuàng)建打印流對象 //PrintWriter pw = new PrintWriter("d.txt"); PrintWriter pw = new PrintWriter(new FileWriter("d.txt"),true); pw.println("hello"); pw.println("world"); pw.println("java"); //釋放資源 //pw.close(); } private static void method() throws FileNotFoundException { //創(chuàng)建打印流對象 PrintWriter pw = new PrintWriter("c.txt"); /*pw.write("hello"); pw.write("world"); pw.write("java");*/ pw.print("hello"); pw.println("world"); pw.println("java"); //釋放資源 pw.close(); } }
- 案例代碼五:
利用打印流將根目錄下的SystemInOutDemo.java復(fù)制到d:\SystemInOutDemo.java下package com.neuedu.demo; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; /* * 使用打印流復(fù)制文本文件 * * 數(shù)據(jù)源 SystemInOutDemo.java BufferedReader * 目的地 d:\\SystemInOutDemo.java PrintWriter * */ public class PrintWriterDemo3 { public static void main(String[] args) throws IOException { //創(chuàng)建輸入流對象 BufferedReader br = new BufferedReader(new FileReader("SystemInOutDemo.java")); //創(chuàng)建打印流對象 PrintWriter pw = new PrintWriter(new FileWriter("d:\\SystemInOutDemo.java"),true); String line;//用于存儲讀取到的每行數(shù)據(jù) while((line = br.readLine()) != null) { pw.println(line); } //釋放資源 pw.close(); br.close(); } }
二、對象操作流
- 概述
用于從流中讀取對象的
ObjectInputStream 稱為 反序列化流,利用輸入流從文件中讀取對象
ObjectOutputStream 稱為 序列化流,利用輸出流向文件中寫入對象
特點:用于操作對象哎迄』赜遥可以將對象寫入到文件中,也可以從文件中讀取對象芬失。
package com.neuedu.demo; /* * 對象操作流:可以用于讀寫任意類型的對象 * ObjectOutputStream * writeObject * ObjectOutputStream(OutputStream out) * ObjectInputStream * readObject * ObjectInputStream(InputStream in) * * 注意: * 使用對象輸出流寫出對象楣黍,只能使用對象輸入流來讀取對象 * 只能將支持 java.io.Serializable 接口的對象寫入流中 * */ public class ObjectOutputStreamDemo2 { public static void main(String[] args) { } }
- 利用序列化流讀寫對象
package com.neuedu.demo; import java.io.Serializable; public class Student implements Serializable { String name; int age; public Student(String name,int age) { this.name = name; this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age +"]"; } }
package com.neuedu.demo; import java.io.EOFException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /* * 使用對象輸出流和讀對象輸入流寫對象 * Exception in thread "main" java.io.NotSerializableException: com.itheima_07.Student * Serializable:序列號,是一個標識接口棱烂,只起標識作用租漂,沒有方法 * 當(dāng)一個類的對象需要IO流進行讀寫的時候,這個類必須實現(xiàn)該接口 * * Exception in thread "main" java.io.EOFException:當(dāng)輸入過程中意外到達文件或流的末尾時颊糜,拋出此異常哩治。 * */ public class ObjectOutputStreamDemo { public static void main(String[] args) throws IOException, ClassNotFoundException { //method(); //創(chuàng)建對象輸入流的對象 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt")); //讀取對象 /*Object obj = ois.readObject(); System.out.println(obj); Object obj2 = ois.readObject(); System.out.println(obj2); Object obj3 = ois.readObject(); System.out.println(obj3);*/ try { while(true) { Object obj = ois.readObject(); System.out.println(obj); } } catch(EOFException e) { System.out.println("讀到了文件的末尾"); } //釋放資源 ois.close(); } private static void method() throws IOException, FileNotFoundException { //創(chuàng)建對象輸出流的對象 //FileOutputStream fos = new FileOutputStream("a.txt"); //ObjectOutputStream oos = new ObjectOutputStream(fos); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt")); //創(chuàng)建學(xué)生對象 Student s = new Student("zhangsan",18); Student s2 = new Student("lisi",19); //寫出學(xué)生對象 oos.writeObject(s); oos.writeObject(s2); //釋放資源 oos.close(); } }
- 案例代碼六:
package com.neuedu.demo; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /* * 使用字符流復(fù)制文本文件 * * 數(shù)據(jù)源 IODemo.java * 目的地 d:\\IODemo.java * */ public class FileCopyDemo { public static void main(String[] args) throws IOException { //創(chuàng)建字符輸入流對象 FileReader fr = new FileReader("IODemo.java"); //創(chuàng)建字符輸出流對象 FileWriter fw = new FileWriter("d:\\IODemo.java"); //一次讀寫一個字符 /*int ch; while((ch = fr.read()) != -1) { fw.write(ch); fw.flush(); }*/ //一次讀寫一個字符數(shù)組 int len;//用于存儲讀到的字符個數(shù) char[] chs = new char[1024]; while((len = fr.read(chs)) != -1) { fw.write(chs,0,len); fw.flush(); } //釋放資源 fw.close(); fr.close(); } }
- 解決對象輸入流讀取對象出現(xiàn)異常的問題
- 案例代碼七:
package com.neuedu.demo; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; /* * 解決對象輸入流讀取對象出現(xiàn)異常的問題 * */ public class ObjectOutputStreamDemo3 { public static void main(String[] args) throws IOException, ClassNotFoundException { //method(); //創(chuàng)建對象輸入流的對象 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("b.txt")); //讀取數(shù)據(jù) Object obj = ois.readObject(); //System.out.println(obj); //向下轉(zhuǎn)型,獲取具體的子類對象 ArrayList<Student> list = (ArrayList<Student>) obj; for (Student student : list) { System.out.println(student); } //釋放資源 ois.close(); } private static void method() throws IOException, FileNotFoundException { //創(chuàng)建對象輸出流的對象 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("b.txt")); //創(chuàng)建集合對象 ArrayList<Student> list = new ArrayList<Student>(); //添加學(xué)生對象 list.add(new Student("wangwu",30)); list.add(new Student("zhaoliu",28)); //寫出集合對象 oos.writeObject(list); //釋放資源 oos.close(); } }
- 解決讀寫對象版本不一致問題
- 案例代碼八:
package com.neuedu.demo; import java.io.Serializable; public class Student implements Serializable { private static final long serialVersionUID = 6361890890437825953L; String name; int age; String gender; public Student(String name,int age) { this.name = name; this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", gender=" + gender + "]"; } }
package com.neuedu.demo; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /* * 解決對實現(xiàn)序列化接口出現(xiàn)的黃色警告問題 * Exception in thread "main" java.io.InvalidClassException * 當(dāng) Serialization 運行時檢測到某個類具有以下問題之一時衬鱼,拋出此異常业筏。 * 該類的序列版本號與從流中讀取的類描述符的版本號不匹配 * 該類包含未知數(shù)據(jù)類型 * 該類沒有可訪問的無參數(shù)構(gòu)造方法 * */ public class ObjectOutputStreamDemo4 { public static void main(String[] args) throws IOException, ClassNotFoundException { //method(); method2(); } //讀取學(xué)生對象 private static void method2() throws IOException, FileNotFoundException, ClassNotFoundException { //創(chuàng)建對象輸入流的對象 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c.txt")); //讀取對象 Object obj = ois.readObject(); System.out.println(obj); //釋放資源 ois.close(); } //寫出學(xué)生對象 private static void method() throws IOException, FileNotFoundException { //創(chuàng)建對象輸出流的對象 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c.txt")); //創(chuàng)建的學(xué)生對象 Student s = new Student("qianqi",28); //寫出學(xué)生對象 oos.writeObject(s); //釋放資源 oos.close(); } }
三、Properties集合
- Properties介紹
Properties 類表示了一個持久的屬性集鸟赫。Properties 可保存在流中或從流中加載蒜胖。屬性列表中每個鍵及其對應(yīng)值都是一個字符串。
特點:
1抛蚤、Hashtable的子類台谢,map集合中的方法都可以用。
2岁经、該集合沒有泛型朋沮。鍵值都是字符串。
3缀壤、它是一個可以持久化的屬性集樊拓。鍵值可以存儲到集合中,也可以存儲到持久化的設(shè)備(硬盤塘慕、U盤筋夏、光盤)上。鍵值的來源也可以是持久化的設(shè)備苍糠。
4叁丧、有和流技術(shù)相結(jié)合的方法。
方法.png
- 利用Properties存儲鍵值對
- 案例代碼九:
package com.neuedu.demo; import java.util.Map; import java.util.Properties; import java.util.Set; /* * Properties:表示了一個持久的屬性集岳瞭,屬性列表中每個鍵及其對應(yīng)值都是一個字符串 * * 構(gòu)造方法: * Properties() */ public class PropertiesDemo2 { public static void main(String[] args) { //創(chuàng)建屬性列表對象 Properties prop = new Properties(); //添加映射關(guān)系 prop.put("CZBK001", "zhangsan"); prop.put("CZBK002", "lisi"); prop.put("CZBK003", "wangwu"); //遍歷屬性列表 //獲取所有的key拥娄,通過key獲取value Set<Object> keys = prop.keySet(); for (Object key : keys) { Object value = prop.get(key); System.out.println(key + "=" + value); } System.out.println("------------------"); //獲取所有的結(jié)婚證對象 Set<Map.Entry<Object,Object>> entrys = prop.entrySet(); for (Map.Entry<Object, Object> entry : entrys) { Object key = entry.getKey(); Object value = entry.getValue(); System.out.println(key + "=" + value); } } }
- Properties與流結(jié)合使用
- 案例代碼十:
package com.neuedu.demo; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Properties; /* * Properties和IO流結(jié)合的功能: * void load(Reader reader) * * void list(PrintWriter out) * void store(Writer writer, String comments) * * */ public class PropertiesDemo2 { public static void main(String[] args) throws IOException{ //method(); //method2(); //創(chuàng)建屬性列表對象 Properties prop = new Properties(); //添加映射關(guān)系 prop.setProperty("CZBK001", "zhangsan"); prop.setProperty("CZBK002", "lisi"); prop.setProperty("CZBK003", "wangwu"); //創(chuàng)建輸出流對象 FileWriter fw = new FileWriter("e.txt"); //void store(Writer writer, String comments) prop.store(fw, "hello world"); //釋放資源 fw.close(); } private static void method2() throws FileNotFoundException, IOException { //創(chuàng)建屬性列表對象 Properties prop = new Properties(); //創(chuàng)建一個輸入流對象 FileReader fr = new FileReader("d.txt"); //void load(Reader reader) prop.load(fr); //釋放資源 fr.close(); System.out.println(prop); } private static void method() throws FileNotFoundException { //創(chuàng)建屬性列表對象 Properties prop = new Properties(); //添加映射關(guān)系 prop.setProperty("CZBK001", "zhangsan"); prop.setProperty("CZBK002", "lisi"); prop.setProperty("CZBK003", "wangwu"); //創(chuàng)建打印流對象 PrintWriter out = new PrintWriter("d.txt"); //void list(PrintWriter out) prop.list(out); //釋放資源 out.close(); } }
四、編碼表
- 編碼表的概述
編碼表:把計算機底層的二進制數(shù)據(jù)轉(zhuǎn)換成我們能看到的字符
ASCII
GB2312 --- GBK
Unicode 所有的字符都占2個字節(jié)
UTF-8 長度可變的碼表ANSI:本地編碼表 gbk
Java中的字符串默認使用的ANSI(gbk)亂碼:編碼保持前后一致即可解決
- Java中字符串的編碼
- 常用方法
- 構(gòu)造方法(字節(jié)數(shù)組轉(zhuǎn)字符串):
String():初始化一個新創(chuàng)建的 String 對象瞳筏,使其表示一個空字符序列
String(byte[] bytes) 使用平臺的默認字符集解碼指定的 byte 數(shù)組稚瘾,構(gòu)造一個新的 String
String(byte[] bytes, Charset charset) 通過使用指定的 charset 解碼指定的 byte 數(shù)組,構(gòu)造一個新的 String- 成員方法(字符串轉(zhuǎn)字節(jié)數(shù)組)
getBytes() 使用平臺的默認字符集將此 String 編碼為 byte 序列姚炕,并將結(jié)果存儲到一個新的 byte 數(shù)組中
getBytes(Charset charset) 使用給定的 charset 將此 String 編碼到 byte 序列摊欠,并將結(jié)果存儲到新的 byte 數(shù)組
- 案例代碼十一
package com.neuedu.demo; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; /* * 編碼表:把計算機底層的二進制數(shù)據(jù)轉(zhuǎn)換成我們能看到的字符 * ASCII * * GB2312 --- GBK * Unicode 所有的字符都占2個字節(jié) * UTF-8 長度可變的碼表 * * ANSI:本地編碼表 gbk * Java中的字符串默認使用的ANSI(gbk) * * 亂碼:編碼保持前后一致即可解決 * */ public class EncoderDemo { public static void main(String[] args) throws IOException { //method(); FileInputStream fis = new FileInputStream("a.txt"); byte[] bys = new byte[1024]; int len = fis.read(bys); //System.out.println(new String(bys,0,len)); System.out.println(new String(bys,0,len,"UTF-8")); } private static void method() throws UnsupportedEncodingException, FileNotFoundException, IOException { String s = "高薪就業(yè)"; //byte[] bys = s.getBytes();//通過默認編碼轉(zhuǎn)換成數(shù)組 byte[] bys = s.getBytes("UTF-8"); FileOutputStream fos = new FileOutputStream("a.txt"); fos.write(bys); fos.close(); } }
- 字符流中的編碼
- 常見對象
- InputStreamReader(InputStream in, CharsetDecoder dec) 創(chuàng)建使用給定字符集解碼器的 InputStreamReader
- OutputStreamWriter(OutputStream out, CharsetEncoder enc) 創(chuàng)建使用給定字符集編碼器的 OutputStreamWriter
- 案例代碼十二
package com.neuedu.demo; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; /* * 字符流中的編碼 * * 字符流 = 字節(jié)流 + 編碼 * */ public class EncoderDemo2 { public static void main(String[] args) throws IOException { //method(); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("b.txt"),"UTF-8"); //OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("b.txt")); String s = "迎娶白富美"; osw.write(s); osw.close(); } private static void method() throws IOException, UnsupportedEncodingException { FileWriter fw = new FileWriter("b.txt"); String s = "月薪過萬"; byte[] bys = s.getBytes("UTF-8"); //fw.write(s); fw.write(new String(bys)); fw.close(); } }