Loading

处理流_小记

RandomAccessFile:随机访问流:此类的实例支持对随机访问文件的读取和写入
不是实际意义上的流,因为它继承自Object类
常用的构造方法:
public RandomAccessFile(String name, String mode)
参数一:指定该文件的路径
参数二:指定的一种模式:常用的模式:”rw”,这种模式是可以读也可以写

package Day19_System;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @author Aoman_Hao
 */
public class RandomTest {

    public static void main(String[] args) throws IOException{
        // 创建RandomAccessFile
        RandomAccessFile raf = new RandomAccessFile(
                "d:\\Java\\JavaTest\\Aoman_Hao.txt", "rw");

        //遍历循环读数据
        for(int i=0;i<raf.length();i++){
        byte readByte = raf.readByte();

        System.out.println(readByte);
        System.out.println(raf.getFilePointer());
        }
        //写数据
        raf.write("12,33,44".getBytes());//string 转字节
        raf.write(123);//

    }

}

SequenceInputStream:合并流(读数据):表示其他输入流的逻辑串联
合并流在复制文件的时候,只能操作数据源,不能操作目的地
*
现在有合并流(将两个文件中的内容复制到另一个文件中)
* a.txt+b.txt—>c.txt文件中
* 构造方法:
* public SequenceInputStream(InputStream s1,InputStream s2)

合并两个文件数据
package Day19_System;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;

/**
 * @author Aoman_Hao
 */
public class SequenceTest {

    public static void main(String[] args) throws IOException{

        // 读数据源文件
        FileInputStream fis1 = new FileInputStream(
                "d:\\Java\\JavaTest\\Aoman_Hao.txt");
        FileInputStream fis2 = new FileInputStream(
                "d:\\Java\\JavaTest\\Aoman_3.txt");

        // sequenceinputstream 合并两个文件
        SequenceInputStream se = new SequenceInputStream(fis1, fis2);

        // 创建写目的文件
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("d:\\Java\\Aoman.txt"));

        //遍历读写数据,单次读取字节数组
        byte[] bys= new byte[1024];
        int len = 0;
        while((len = se.read(bys))!=-1){
            bos.write(bys, 0, len);
            bos.flush();
        }

        bos.close();
        se.close();
        fis1.close();
        fis2.close();
    }

}
合并多个文件数据:
package Day19_System;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

/**
 * @author Aoman_Hao
 */
public class SequenceTest2 {

    public static void main(String[] args) throws IOException {
        // 读取数据文件
        FileInputStream f1 = new FileInputStream(
                "d:\\Java\\JavaTest\\Aoman_Hao.txt");
        FileInputStream f2 = new FileInputStream(
                "d:\\Java\\JavaTest\\Aoman_3.txt");
        FileInputStream f3 = new FileInputStream("d:\\Java\\JavaTest\\aaa.txt");

        Vector<InputStream> ve = new Vector<InputStream>();
        ve.add(f1);
        ve.add(f2);
        ve.add(f3);

        // //调用特有功能:
        // public Enumeration<E> elements()
        Enumeration<InputStream> elements = ve.elements();
        // 创建合并流对象
        SequenceInputStream sis = new SequenceInputStream(elements);
        // 创建合并对象写入文件,目的地
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("d:\\Java\\Aoman_2.txt"));

        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = sis.read(bys)) != -1) {
            bos.write(bys, 0, len);
            bos.flush();
        }
            bos.close();
            sis.close();
    }

}

posted @ 2017-11-30 22:00  AomanHao  阅读(21)  评论(0)    收藏  举报