标签:style blog io ar color os 使用 sp for
1 打印流:字节流:PrintStream 字符流: PrintWriter
package lianxi1; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import org.junit.Test; public class TestOtherStream { @Test public void test1(){ FileOutputStream fos = null; try { fos = new FileOutputStream(new File("Tangshan")); } catch (FileNotFoundException e) { e.printStackTrace(); } PrintStream ps = new PrintStream(fos,true); if(ps!=null){ System.setOut(ps); } for(int i=1;i<=300;i++){ System.out.print((char)i); if(i%50==0){ System.out.println(); } } try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2 数据流:用来处理基本数据类型、String、字节数组的数据:DateInputStream,DateOutputStream
package lianxi1; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import org.junit.Test; public class TestOtherStream { @Test public void test2(){ DataOutputStream dos = null; DataInputStream dis = null; try { FileOutputStream fos = new FileOutputStream("data"); dos = new DataOutputStream(fos); dos.writeDouble(32.23); dos.writeUTF("我是年轻人"); dos.writeInt(3234); FileInputStream fis = new FileInputStream("data"); dis = new DataInputStream(fis); double d1 = dis.readDouble(); System.out.println(d1); String s1 = dis.readUTF(); System.out.println(s1); int i1 = dis.readInt(); System.out.println(i1); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { dos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Test public void test1(){ FileOutputStream fos = null; try { fos = new FileOutputStream(new File("Tangshan")); } catch (FileNotFoundException e) { e.printStackTrace(); } PrintStream ps = new PrintStream(fos,true); if(ps!=null){ System.setOut(ps); } for(int i=1;i<=300;i++){ System.out.print((char)i); if(i%50==0){ System.out.println(); } } try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
3 对象流:
对象的序列化过程:用ObjectOutputStream类将一个java对象写入IO流中
反序列化过程: 用ObjectInputStream类从IO流中恢复该Java对象
要实现序列化的类:1 要求此类是序列化的,实现Serializable接口
2 要求类的属性同样要实现Serializable接口
3 提供一个版本号: private static final long serialVersionUID
4 使用static或transient修饰的属性,不可实现序列化
标签:style blog io ar color os 使用 sp for
原文地址:http://www.cnblogs.com/yjtm53/p/4158620.html