美文网首页
Java InputStream 和DataInputStrea

Java InputStream 和DataInputStrea

作者: 柳源居士 | 来源:发表于2018-03-02 22:04 被阅读1941次
    1. DataInputStream 继承于FilterInputStream ,FilterInputStream继承于InputStream。因此可以说InputSteam是DataInputStream的父类。
    2. DataInputStream 实现了 DataInput的接口。
      重载了以下方法(并设置为final):
    public interface DataInput {
        void readFully(byte[] var1) throws IOException;
    
        void readFully(byte[] var1, int var2, int var3) throws IOException;
    
        int skipBytes(int var1) throws IOException;
    
        boolean readBoolean() throws IOException;
    
        byte readByte() throws IOException;
    
        int readUnsignedByte() throws IOException;
    
        short readShort() throws IOException;
    
        int readUnsignedShort() throws IOException;
    
        char readChar() throws IOException;
    
        int readInt() throws IOException;
    
        long readLong() throws IOException;
    
        float readFloat() throws IOException;
    
        double readDouble() throws IOException;
    
        String readLine() throws IOException;
    
        String readUTF() throws IOException;
    }
    
    1. DataInputStream 重载了FilterInputStream 部分的方法,并设置为final 类型方法,可以继承DataInputStream 类,但是无法重载父类方法。
      DataInputStream 方法:
    public class DataInputStream extends FilterInputStream implements DataInput {
        public DataInputStream(InputStream in) {
            super((InputStream)null);
            throw new RuntimeException("Stub!");
        }
    
        public final int read(byte[] b) throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final int read(byte[] b, int off, int len) throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final void readFully(byte[] b) throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final void readFully(byte[] b, int off, int len) throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final int skipBytes(int n) throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final boolean readBoolean() throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final byte readByte() throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final int readUnsignedByte() throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final short readShort() throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final int readUnsignedShort() throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final char readChar() throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final int readInt() throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final long readLong() throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final float readFloat() throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final double readDouble() throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        /** @deprecated */
        @Deprecated
        public final String readLine() throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public final String readUTF() throws IOException {
            throw new RuntimeException("Stub!");
        }
    
        public static final String readUTF(DataInput in) throws IOException {
            throw new RuntimeException("Stub!");
        }
    }
    
    1. DataInputStream (数据输入流)允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。
      下面的构造方法用来创建数据输入流对象。
    DataInputStream dis = new DataInputStream(InputStream in);
    

    方法描述:

    序号 方法描述
    1 public final int read(byte[] r, int off, int len)throws IOException
    从所包含的输入流中将 len 个字节读入一个字节数组中。如果len为-1,则返回已读字节数。
    2 Public final int read(byte [] b)throws IOException
    从所包含的输入流中读取一定数量的字节,并将它们存储到缓冲区数组 b 中。
    3 1. public final Boolean readBooolean()throws IOException,
    2. public final byte readByte()throws IOException,
    3. public final short readShort()throws IOException
    4. public final Int readInt()throws IOException
    从输入流中读取字节,返回输入流中两个字节作为对应的基本数据类型返回值。
    4 public String readLine() throws IOException
    从输入流中读取下一文本行。
    1. 实例

    下面的例子演示了DataInputStream和DataOutputStream的使用,该例从文本文件test.txt中读取5行,并转换成大写字母,最后保存在另一个文件test1.txt中。
    test.tx 文件内容如下:

    runoob1
    runoob2
    runoob3
    runoob4
    runoob5

    代码:

    import java.io.*;
     
    public class Test{
       public static void main(String args[])throws IOException{
     
          DataInputStream in = new DataInputStream(new FileInputStream("test.txt"));
          DataOutputStream out = new DataOutputStream(new  FileOutputStream("test1.txt"));
          BufferedReader d  = new BufferedReader(new InputStreamReader(in));
         
          String count;
          while((count = d.readLine()) != null){
              String u = count.toUpperCase();
              System.out.println(u);
              out.writeBytes(u + "  ,");
          }
          d.close();
          out.close();
       }
    }
    
    运行结果
    RUNOOB1
    RUNOOB2
    RUNOOB3
    RUNOOB4
    RUNOOB5

    6. 通过实例可以看出流程:

    • 创建InputStream对象(所有InputStream的子类对象,可以是文件输入流等)。
    • 创建DataInputStream 对象,构造参数为 InputStream 对象
    • 创建 InputStreamReader 对象,构造参数为DataInputStream 对象
    • 创建 BufferedReader 对象,构造参数为 InputStreamReader 对象
    • 最终通过BufferedReader 对象来操作 文件输入流 数据。

    相关文章

      网友评论

          本文标题:Java InputStream 和DataInputStrea

          本文链接:https://www.haomeiwen.com/subject/aerzxftx.html