Binary IO

FileInputStream

  • 2 Constructors
    • public FileInputStream ( String filename)
    • public FileInputStream ( File file)

FileOutputStream

  • 4 Constructors
    • public FileOutputStream ( String filename)
    • public FileOutputStream ( File file)
    • public FileOutputStream ( String filename, Boolean append)
    • public FileOutputStream ( File file, Boolean append)

DataInputStream

  • Constructor
    public DataInputStream ( InputStream instream)
  • Example:
    DataInputStream infile = new DataInputStream ( new FileInputStream ( "in.dat" ) ) ;
    infile.readBoolean ( ) ;
    infile.readByte ( ) ;
    infile.readBytes ( ) ;    // Read lower byte of characters
    infile.readChar ( ) ;
    infile.readChars ( ) ;    // Read every character
    infile.readFloat ( ) ;
    infile.readDouble ( ) ;
    infile.readInt ( ) ;

    ...

DataOutputStream

  • Constructor
    public DataOutputStream ( OutputStream outstream)
  • Example:
    DataOutputStream outfile = new DataOutputStream ( new FileOutputStream ( "out.dat" ) ) ;
    outfile.writeBoolean ( boolTmp) ;
    outfile.writeByte ( intTmp) ;
    outfile.writeBytes ( strTmp) ;    // Write lower byte of characters
    outfile.writeChar ( charTmp) ;
    outfile.writeChars ( strTmp) ;    // Write every character
    outfile.writeFloat ( floatTmp) ;
    outfile.writeDouble ( dblTmp) ;
    outfile.writeInt ( intTmp) ;

    ...

BufferedInputStream/ BufferedOutputStream

  • Using buffers to speed up I/O
  • Constructor
    public BufferedInputStream ( InputStream in)
    public BufferedInputStream ( InputStream in, int bufferSize)
  • Constructor
    public BufferedOutputStream ( OutputStream out)
    public BufferedOutputStream ( OutputStreamr out, int bufferSize)

ObjectInputStream/ ObjectOutputStream

  • Enables to perform I/O for objects
  • Constructor
    public ObjectInputStream ( InputStream in)
  • Constructor
    public ObjectOutputStream ( OutputStream out)

RandomAccessFile

  • to allow a file to be read from and write to at random locations
  • Constructor
    // Allow read and write
    RandomAccessFile raf = new RandomAccessFile ( "test.dat" , "rw" ) ;
    // Read only
    RandomAccessFile raf = new RandomAccessFile ( "test.dat" , "r" ) ;
  • Example
    RandomAccessFile inout = new RandomAccessFile ( "test.dat" , "rw" ) ;

    // Clear the file to destroy the old contents if exists
    inout.setLength ( 0 ) ;
    // Write new integers to the file
    for ( int i = 0 ; i < 200 ; i++ )
        inout.writeInt ( i) ;
    System .out .println ( inout.length ( ) ) ; // length of the file
    inout.seek ( 0 ) ; // Move the file pointer to the beginning
    System .out .println ( inout.readInt ( ) ) ;
    inout.seek ( 1 * 4 ) ; // Move the file pointer to the 2nd number
    System .out .println ( inout.readInt ( ) ) ;
    // Close file
    inout.close ( ) ;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值