水神骑士联盟吧 关注:4贴子:334
  • 1回复贴,共1

关于不常用的io流

只看楼主收藏回复

//随机访问文件流RandomAccessFile
public static void main(String[] args) throws Exception {
RandomAccessFile accessFile = new RandomAccessFile("/home/loop/下载/a.txt", "rw") ;
accessFile.writeUTF("java是一门多线程的语言");//writeUTF方法在字符串前加了连个字节readUTF方法跳过这两个字节
// accessFile.writeInt(100);
// accessFile.writeUTF("java:继续下载");
accessFile.seek(6);//设置文件指针从哪里开始读取(适合做继续下载)
ByteArrayOutputStream out = new ByteArrayOutputStream() ;
int len ;
byte[] buf = new byte[1024*8] ;
while((len = accessFile.read(buf))!=-1){
out.write(buf, 0, len);
}
System.out.println(out.toString());
accessFile.close()
}


IP属地:河北1楼2016-08-16 17:31回复
    //合并流
    public static void main(String[] args) throws Exception {
    //使用方式一
    // InputStream in1 = new FileInputStream("/home/loop/下载/a.txt") ;
    // @SuppressWarnings("resource")
    // InputStream in2 = new FileInputStream("/home/loop/下载/b.txt") ;
    // SequenceInputStream sin = new SequenceInputStream(in1, in2) ;
    //使用方式二
    Vector<InputStream> vector = new Vector<>() ;
    vector.add(new FileInputStream("/home/loop/下载/a.txt")) ;
    vector.add(new FileInputStream("/home/loop/下载/b.txt")) ;
    vector.add(new FileInputStream("/home/loop/下载/c.txt")) ;
    SequenceInputStream sin = new SequenceInputStream(vector.elements()) ;
    OutputStream out = new FileOutputStream("/home/loop/下载/d.txt") ;
    int len ;
    byte[] buf = new byte[1024*8] ;
    while((len = sin.read(buf)) != -1){
    out.write(buf, 0, len);
    }
    out.close();
    sin.close();
    }


    IP属地:河北2楼2016-08-16 18:03
    回复