处理流类型:
1、缓冲流
例1:
import java.io.*; public class TestBufferStream{ public static void main(String args[]){ try{ FileInputStream fis = new FileInputStream("TestCopyByMyself.java"); BufferedInputStream bis = new BufferedInputStream(fis); int c = 0 ; System.out.println((char)bis.read()); System.out.println((char)bis.read()); bis.mark(100);//做标记,没看懂API for(int i=0; i<=10&&(c=bis.read()) != -1; i++){ System.out.print((char)c+" "); } System.out.println(); bis.reset();//回到标记的位置 for(int i=0; i<=10&&(c=bis.read()) != -1; i++){ System.out.print((char)c+" "); } bis.close(); }catch(IOException e){ e.printStackTrace(); } } }
例2:BufferedWriter BufferedRead 应用非常普遍(记住)
import java.io.*; public class TestBufferStream{ public static void main(String args[]){ try{ BufferedWriter bw = new BufferedWriter(new FileWriter("TestCopyByMyself.java")); BufferedReader br = new BufferedReader(new FileReader("TestCopyByMyself.java")); String s = null ; for(int i=0; i<=100; i++){ s = String.valueOf(Math.random());//返回字符串表示形式 bw.write(s); bw.newLine(); } bw.flush(); while((s=br.readLine())!= null){ System.out.println(s); } bw.close(); br.close(); }catch(IOException e){ e.printStackTrace(); } } }
2、转换流(重要)
例1:
import java.io.*; public class TestTransForm{ public static void main(String args[]){ try{ OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("TestCopyByMyself.java")); osw.write("fgatwesdghruklotueo"); System.out.println(osw.getEncoding()); osw.close(); osw = new OutputStreamWriter(new FileOutputStream("TestCopyByMyself.java",true),"ISO8859_1");//true是在原文件基础上追加,否则覆盖原内容 //ISO8859_1是字符编码,默认编码GBK osw.write("fgatwesdghruklotueo"); System.out.println(osw.getEncoding()); osw.close(); }catch(IOException e){ e.printStackTrace(); } } }
例2、典型用法
import java.io.*; public class Test{ public static void main(String args[]){ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = null ; try{ while((s = br.readLine())!=null){ if(s.equalsIgnoreCase("exit")) break; System.out.println(s.toUpperCase()); } br.close(); }catch(IOException e){ e.printStackTrace(); } } }
3、数据流
例1
import java.io.*; public class TestDataStream{ public static void main(String args[]){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); try{ dos.writeDouble(Math.random()); dos.writeBoolean(true); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); System.out.println(bais.available());//字节数=9 DataInputStream dis = new DataInputStream(bais); System.out.println(dis.readDouble()); System.out.println(dis.readBoolean()); dos.close(); dis.close(); }catch(IOException e){ e.printStackTrace(); } } }
输出:
9
0.5303779743857895
true
练习:自己读写文件,注意文件中显示的并非double+boolean
import java.io.*; public class Test{ public static void main(String [] args){ String filename = "data.txt"; try{ DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename)); DataInputStream dis = new DataInputStream(new FileInputStream(filename)); dos.writeDouble(6.7); dos.writeInt(3); System.out.println(dis.available()); System.out.println(dis.readDouble()); System.out.println(dis.readInt()); dos.close(); dis.close(); }catch(IOException e){ e.printStackTrace(); } } }
4、Print流
例1:
import java.io.*; public class TestPrintStream{ public static void main(String args[]){ PrintStream ps = null ; try{ FileOutputStream fos = new FileOutputStream("log.dat"); ps = new PrintStream(fos); }catch(IOException e){ e.printStackTrace(); } if(ps!=null){ System.setOut(ps);//System.out中的out默认是命令行,setOut可以改变out的值,这里out指向ps。 } int ln = 1; for(char c=0;c<=6000;c++){ System.out.print(c+" "); if(ln++>=10){ System.out.println(); ln = 1; } } } }
例2:
import java.io.*; //输入文件名,读该文件内容 public class TestPrintStream{ public static void main(String args[]){ if(args.length<=0) return; String filename = args[0]; if(filename!=null){ list(filename,System.out); } } public static void list(String f, PrintStream fs){ try{ BufferedReader br = new BufferedReader(new FileReader(f)); String s = null; while((s = br.readLine())!=null){ fs.println(s); } br.close(); }catch(IOException e){ fs.println("无法读取文件"); } } }
例3:日志
import java.io.*; import java.util.*; public class TestPrintStream{ public static void main(String args[]){ String s = null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try{ FileWriter fw = new FileWriter("logfile.log",true); PrintWriter log = new PrintWriter(fw); while((s = br.readLine())!=null){ if(s.equalsIgnoreCase("exit")) break; System.out.println(s.toUpperCase()); log.println("-----"); log.println(s.toUpperCase()); log.flush(); } log.println("==="+new Date()+"==="); log.flush(); log.close(); }catch(IOException e){ e.printStackTrace(); } } }
日志的另一种写法(by myself):
import java.io.*; import java.util.*; public class Test{ public static void main(String [] args){ String filename = "log.txt" ; String line = null ; try{ InputStreamReader fr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter(filename); /* PrintWriter pw = new PrintWriter(fw); while((line = br.readLine()) != null){ if(line.equalsIgnoreCase("exit")){ break; } pw.println(line); } pw.write("==="+new Date()+"==="); pw.flush(); pw.close(); */ BufferedWriter bw = new BufferedWriter(fw); while((line=br.readLine()) != null){ if(line.equalsIgnoreCase("exit")){ break; } bw.write(line); bw.newLine(); } bw.write("==="+new Date()+"==="); bw.flush(); bw.close(); }catch(IOException e){ e.printStackTrace(); } } }
5、Object流
直接将Object写入或读出。
必须实现seriallzable接口(序列化),是标记性接口,JDK控制序列化过程。
transient关键字修饰的成员变量在序列化时不考虑,即只写入其他三个,而忽略该成员变量。
externalizable接口(外部化),是seriallzable的子接口。此外还有两个方法,自己控制序列化过程。
例1:
import java.io.*; public class Test{ public static void main(String [] args){ String filename = "obj.txt"; Obj o = new Obj(); try{ FileOutputStream fos = new FileOutputStream(filename); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(o); oos.flush(); oos.close(); FileInputStream fis = new FileInputStream(filename); ObjectInputStream ois = new ObjectInputStream(fis); Obj obj = (Obj)ois.readObject(); System.out.println(obj.a+" "+obj.b+" "+obj.c); }catch(IOException e){ e.printStackTrace(); }catch(ClassNotFoundException e){ e.printStackTrace(); } } } class Obj implements Serializable{ int a = 1 ; String b = "good" ; transient int c = 9 ; }
输出:
1 good 0
总结: