其他流的使用

标准的输入输出流

  1. System.in:标准的输入流,默认从键盘输入
  2. System.out:标准的输出流,默认从控制台输出
  3. 可以通过System的setIn(InputStream)和setOut(PrintfStream)来重写指定输入输出的流

    打印流

  4. printStream

    1. 可以setOut()替换Sys.out这个打印流
  5. PrintWriter

    数据流

  6. DataInputStream和DataOutputStream

    1. radeBoloean()
    2. readByte()等
  7. 读取或写出基本数据类型的字符串,字节流套在里面

    对象流

  8. ObjectInputStream和ObjectOutputStream

  9. 用于存储和读取基本数据类型和对象的处理流,可以序列化和反序列化对象
  10. 序列化:保存对象
  11. 反序列化,读取对象到内存
  12. 不能序列化Stataic和transient

    自定义类的序列化

  13. 实现sarializable

  14. 实现externalizable
  15. 提供一个public static final long sarialVersinUid
  16. 自定义对象的属性也必须是可序列化的(String,基本数据类型)
  17. static和transient不可序列化

    1. //对象流
    2. ObjectOutputStream out = null;
    3. ObjectInputStream in = null;
    4. try {
    5. FileOutputStream fileOut = new FileOutputStream("object.txt");
    6. out = new ObjectOutputStream(fileOut);
    7. out.writeObject(new String("对象流测试"));
    8. out.flush();
    9. FileInputStream fileIn = new FileInputStream("object.txt");
    10. in = new ObjectInputStream(fileIn);
    11. String result = (String)in.readObject();
    12. System.out.println(result);
    13. } catch (IOException e) {
    14. // TODO Auto-generated catch block
    15. e.printStackTrace();
    16. } catch (ClassNotFoundException e) {
    17. // TODO Auto-generated catch block
    18. e.printStackTrace();
    19. }finally{
    20. if(out!=null){
    21. try {
    22. out.close();
    23. } catch (IOException e) {
    24. // TODO Auto-generated catch block
    25. e.printStackTrace();
    26. }
    27. }
    28. if(in != null){
    29. try {
    30. in.close();
    31. } catch (IOException e) {
    32. // TODO Auto-generated catch block
    33. e.printStackTrace();
    34. }
    35. }
    36. }

    随机存取文件流

  18. RandomAccessFile

  19. 实现了DataOutput和DataInput接口,即可以读,也可以写
  20. seek(pointer)设定指针

    扩展

  21. ByteArrayOutputStream
    write()不输出,最后toString()获取结果;

    Java NIO

  22. 1.4引入的一套IO API,可以替代标准的Java IO API,NIO是面向缓冲区buffer的(区别于流),基于通道channel的IO操作。

  23. 有两套NIO,一套是标准输入输出NIO,另一套是网络编程NIO.
  24. java.nio.channels.channel
    Filechannel 处理本次文件
    SocketChannel TCP网络编程的客户端channel
    serverSocketChannel 服务器端channel
    DatagramChannel UDp网络编程中发送端和接收端的channel
  25. Path、Paths、Files