IO流(流?) 1.0 概念和分類 2.0 位元組輸出流 (1)輸入一個位元組 import java.io.FileOutputStream; import java.io.IOException; public class Main{ public static void main(String[] ...
IO流(流?)
1.0 概念和分類
2.0 位元組輸出流
(1)輸入一個位元組
import java.io.FileOutputStream; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileOutputStream fos=new FileOutputStream("Demo01\\a.txt"); fos.write(97); fos.close(); } }
(2)多個位元組的輸入
import java.io.FileOutputStream; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileOutputStream fos=new FileOutputStream("Demo01\\b.txt"); byte bytes[]={65,66,67,68};//ABCD //第一種 fos.write(bytes); //第二種 fos.write(bytes,1,3); //第三種 String s="你好,很高興認識你"; byte[] bytes1 = s.getBytes(); System.out.println(bytes1);//[B@1c53fd30 fos.write(bytes1); fos.close(); } }
位元組輸出流的續寫和換行
import java.io.FileOutputStream; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileOutputStream fos=new FileOutputStream("Demo01\\c.txt",true); fos.write("我說很高興我能遇見你".getBytes()); for (int i = 0; i <5 ; i++) { fos.write("我也是".getBytes()); fos.write("\r\n".getBytes()); } fos.close(); } }
3.位元組輸入流
單個字元讀取
import java.io.FileInputStream; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("Demo01\\b.txt"); int len=0; while((len=fis.read())!=-1) { System.out.print(len+" "); } fis.close(); } }
列印結果:
97 98 99
多個字元的讀取
import java.io.FileInputStream; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("Demo01\\b.txt"); byte []bytes=new byte[1024]; int len=0; while((len=fis.read(bytes))!=-1) { System.out.println(len); String s = new String(bytes, 0, len); System.out.println(s); } } }
列印結果:
8
ABCDEabc
4.字元輸入流
import java.io.FileReader; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileReader fr=new FileReader("Demo01\\b.txt"); int len=0; /*讀取一個字元 while ((len=fr.read())!=-1) { System.out.print((char)len); }*/ /*讀取多個字元 char chars[]=new char[1024]; while((len=fr.read(chars))!=-1) { System.out.println(new String(chars,0,len)); }*/ fr.close(); } }
5.字元輸出流
import java.io.FileWriter; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileWriter fw=new FileWriter("Demo01\\b.txt"); fw.write(97); fw.flush(); //fw.close(); } }
flush()和close()的區別
位元組輸出流的續寫和換行
6.try...catch()處理流中的異常
import java.io.FileWriter; import java.io.IOException; public class Main{ public static void main(String[] args) { FileWriter fs=null; try{ fs=new FileWriter("Dem01\\b.txt"); for(int i=0;i<5;i++) { fs.write("你好"+i+"\r\n"); } } catch(IOException e) { System.out.println(e); } finally { if(fs!=null) { try { fs.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
JDK 7對其優化
6.屬性集
import java.util.Enumeration; import java.util.Properties; import java.util.Set; public class Main{ public static void main(String[] args) { Properties prop=new Properties(); prop.setProperty("Jin","27"); prop.setProperty("V","25"); prop.setProperty("JK","25"); Set<String> set = prop.stringPropertyNames(); for (String s : set) { System.out.println(prop.get(s)); } } }
store方法
import java.io.FileWriter; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; import java.util.Set; public class Main{ public static void main(String[] args) throws IOException { Properties prop=new Properties(); prop.setProperty("Jin","27"); prop.setProperty("V","25"); prop.setProperty("JK","23"); prop.store(new FileWriter("Demo01\\b.txt"),"practice"); } }
load方法
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Properties; import java.util.Set; public class Main{ public static void main(String[] args) throws IOException { Properties prop=new Properties(); prop.load(new FileReader("Demo01\\b.txt")); Set<String> set = prop.stringPropertyNames(); for (String s : set) { System.out.println(s+"="+prop.getProperty(s)); } } }
緩衝流
1.0 位元組緩衝輸出流
2.位元組緩衝輸入流
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("Demo01\\c.txt"); BufferedInputStream input=new BufferedInputStream(fis); int len=0; byte []bytes=new byte[1024]; while((len=input.read(bytes))!=-1) { System.out.println(new String(bytes,0,len)); } input.close(); } }
3.字元緩衝輸出流
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { BufferedWriter bw=new BufferedWriter(new FileWriter("Demo01\\c.txt")); for(int i=0;i<5;i++) { bw.write("你好,我叫赤木晴子"); bw.newLine(); } bw.flush(); bw.close(); } }
列印結果:
4.字元緩衝輸入流
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new FileReader("Demo01\\c.txt")); String s; while((s=br.readLine())!=null) { System.out.println(s); } br.close(); } }
轉換流
1.0 OutputStreamWrite
2.InputStreamReader
轉換流的練習
將GBK文件轉化為UTF-8文件
import java.io.*; public class Main{ public static void main(String[] args) throws IOException { InputStreamReader isr=new InputStreamReader(new FileInputStream("Demo01\\我是一個GBK文件.txt"),"GBK"); OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("Demo01\\轉化後的UTF-8文件"),"UTF-8"); int len=0; while((len=isr.read())!=-1) { osw.write(len); } isr.close(); osw.close(); } }
序列化與非序列化
序列化
ObjectOutputStream
import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Main{ public static void main(String[] args) throws IOException { ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("Demo01\\a.txt")); oos.writeObject(new Person("百裡玄策",18)); oos.close(); } }
ObjectInputStream
import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class Main{ public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream ois=new ObjectInputStream(new FileInputStream("Demo01\\a.txt")); Object o = ois.readObject(); ois.close(); System.out.println(o); } }
transient關鍵字修飾的成員變數不能被序列化(瞬態關鍵字)
import java.io.Serializable; public class Person implements Serializable { private String name; private transient int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } public void setAge(int age) { this.age = age; } public Person() { } }
import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class Test { public static void main(String[] args) throws IOException,ClassNotFoundException { ObjectInputStream ois=new ObjectInputStream(new FileInputStream("Demo01\\a.txt")); Object o = ois.readObject(); ois.close(); System.out.println(o); } }
列印結果:
Person{name='百裡玄策', age=0}
列印流
import java.io.FileNotFoundException; import java.io.PrintStream; public class Main{ public static void main(String[] args) throws FileNotFoundException { System.out.println("我在控制台輸出"); PrintStream ps=new PrintStream("輸出流"); System.setOut(ps); System.out.println("我在輸出流中輸出"); } }