美文网首页
Java IO 小结

Java IO 小结

作者: liut_2016 | 来源:发表于2016-09-29 16:00 被阅读21次
  1. RandomAccessFile
    文件访问类,可读写文件的任意位置
    RandomAccessFile raf = new RandomAccessFile(file,"rw") rw&r 读写&只读

    写方法

        raf.write(int b);       //只写一个字节,同时指针指向下一个位置,准备再次写入
        raf.write(byte b[]);     //把byte b[] 写入
        raf.write(byte b[], int off, int len);  //写入b的指定长度
    

    读方法

        //返回int
        raf.read();                       //读取一个字节
        read(byte b[])                     //读取到byte b
        read(byte b[], int off, int len)    //读取指定长度到b
    

    其它方法

        raf.getFilePointer()       //获取指针
        raf.seek(long pos);     //设置指针
        raf.close();            //关闭
    
  2. IO 抽象基类 △
    inputStream
    outputStream
    输入流基本方法

        is.read();                          //读取一个字节无符号填充到int低八位
        is.read(bytes);                     //读取数据到bytes
        is.read(bytes, 0, bytes.length);    //读取数据到字节数组中,从0开始放,最多放length个,返回读取字节个数

输出流基本方法

        os.write(b);                        //把b(int低八位)写到流
        os.write(bytes);                    //把byte(byte数组)写到流
        os.write(bytes, 0, bytes.length);   //把byte(byte数组)指定长度写到写到流

应用


        //文件复制
        InputStream is = new FileInputStream("/home/liut/桌面/ubuntu-16.04-desktop-amd64.iso");
        OutputStream os = new FileOutputStream("/home/liut/桌面/123.iso");
        byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf, 0, buf.length)) != -1) {
            os.write(buf,0,len);
            os.flush();
        }
        is.close();
        os.close();

相关文章

网友评论

      本文标题:Java IO 小结

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