Java(9-1)输入与输出(二)

上节我们关注了 文本形式的读入和写出的操作(UTF-8编码)。 这回,我们要看看在Java中如何对文件以 010101 二进制的形式 来进行读写 。

Part 1.1 DataInput 和 DataOutput接口的介绍

DataOutput接口中定义了用二进制格式读取,数组、字符、boolean值和字符串的方法; 例如,writeInt总是将一个正数写出为4字节的二进制数量值,而不管他有多少位,writeDouble总是将一个double值写出为8字节的二进制数量值。 这样子会比解析文本要快!

writeByte 读取一个字节
writeInt  读取四个字节(一般说)
...

而为了都会数据,可以使用DataInput接口中定义的一些类似的方法

readInt ...
readByte ...
...

DataInputStream类实现了DataInput接口,为了从文件中读入二进制数据,可以将DataInputStream与某个字节源相结合,例如FileInputStream

DataInputStream in = new DataInputStream(new FileInputStream("employee.dat"));

与此类似,要想写出二进制数据,我们可以使用实现了DataOutput接口的DataOutputStream类:

DataOutputStram out = new DataOutputStream(new FileOutputStream("employee.dat"));

Part 2.1 随机访问文件(可用于多线程复制文件)

RandomAccessFile类可以在文件中的任何位置查找或写入数据。磁盘文件都是随机访问的,但是网络套接字通信的输入\输出刘不是!你可以打开一个随机访问文件只用于读入或者同时用于读写,你可以通过字符串 r 或 rw 作为构造器的第二个参数来指定这个选项。

RandomAccessFile in = new RandomAccessFile("employee.dat","r");
RandomAccessFile inOut = new RandomAccess("employee.dat","rw");

当你将已有文件作为RandomAccessFile打开时,这个文件并不会删除。

随机访问文件有一个表示下一个将被读入或写出的字节所处位置的文件指针,seek方法可用来将这个文件指针设置到文件的任意字节位置,seek的参数是一个long型的整数,他的值位于0到文件的字节长度之间 。

getFilePointer方法将放回文件指针当前的位置。

RandomAccessFile类同时实现了DataInput和DataOutput接口。为了读写,我们可以使用前面说过的 readInt writeInt 之类的方法 。

下面的代码是以二进制的形式进行读和写:

package readAndwriteBy10;

import java.io.*;
import TextFileTest.Employee;

public class ReadWrite10 {
    /**
     * java二进制形式写出数据
     */
    public void wirteBinary() {  
        try {  
            DataOutputStream os = new DataOutputStream(  
                    new BufferedOutputStream(new FileOutputStream(  
                            "E:\\data.dat")));  
            os.writeInt(1001);  
            os.writeByte(520);  
            os.writeBoolean(true);  
            os.writeFloat(10.0f);  
            os.writeLong(100l);  
            os.writeUTF("读写二进制文件");  

            os.flush();  
            os.close();  

        } catch (IOException e) {  

        }  
    }  
    /**
     * java以二进制形式读入数据
     */
    public void readBinary() {  
        try {  
            DataInputStream is = new DataInputStream(  
                    new BufferedInputStream(new FileInputStream(  
                            "E:\\data.dat")));  
            System.out.println(is.readInt());  
            System.out.println(is.readByte());  
            System.out.println(is.readBoolean());  
            System.out.println(is.readFloat());  
            System.out.println(is.readLong());  
            System.out.println(is.readUTF());  

            is.close();  
        } catch (IOException e) {  

        }  
    }  
}

下面的代码是,应用随机访问文件类,来写的一个多线程复制文件程序

package mastercn;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;


public class CopyFile implements Runnable {
    //来源文件
    private String sourceFileName;
    //目标文件
    private String targetFileName;
    //分块总数
    private int blockCount;
    //开始COPY的块序号
    private int blockNo;
    //缓存大小
    private int maxBuffSize=1024*1024;
    /**
     * 将sourceFileName文件分blockCount块后的第blockNo块复制至sourceFileName
     * @param sourceFileName 来源文件全名
     * @param targetFileName 目标文件全名
     * @param blockCount 文件分块COPY数
     * @param blockNo 开始COPY的块序号
     */
    public CopyFile(String sourceFileName,String targetFileName,int blockCount,int blockNo)
    {
        this.sourceFileName=sourceFileName;
        this.targetFileName=targetFileName;
        this.blockCount=blockCount;
        this.blockNo=blockNo;
    }
    public void run() {
        //得到来源文件
        File file=new File(sourceFileName);
        //得到来源文件的大小
        long size=file.length();
        //根据文件大小及分块总数算出单个块的大小
        long blockLenth=size/blockCount;
        //算出当前开始COPY的位置
        long startPosition=blockLenth*blockNo;
        //实例化缓存
        byte[] buff=new byte[maxBuffSize];
        try{
            //从源文件得到输入流
            InputStream inputStream=new FileInputStream(sourceFileName);
            //得到目标文件的随机访问对象
            RandomAccessFile raf=new RandomAccessFile(targetFileName,"rw");
            //将目标文件的指针偏移至开始位置
            raf.seek(startPosition);
            //当前读取的字节数
            int curRedLength;
            //累计读取字节数的和
            int totalRedLength=0;
            //将来源文件的指针偏移至开始位置
            inputStream.skip(startPosition);
            //依次分块读取文件
            while((curRedLength=inputStream.read(buff))>0 && totalRedLength<blockLenth)
            {
                //将缓存中的字节写入文件?目标文件中
                raf.write(buff, 0, curRedLength);
                //累计读取的字节数
                totalRedLength+=curRedLength;
            }
            //关闭相关资源
            raf.close();
            inputStream.close();
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

}
 ExpandedBlockStart.gif
package mastercn;

public class Test {

    /**
     * @param args
     */
    //来源文件
    private static String sourceFile;
    //目标文件
    private static String targetFile;
    //分块数
    private static int blockCount;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //
        sourceFile=args[0];
        targetFile=args[1];
        blockCount=Integer.parseInt(args[2]);
        //记录开始时间
        long beginTime=System.currentTimeMillis();
        //依次分块进行文件COPY
        for(int i=0;i<blockCount;i++)
        {
            //实例化文件复制对象
            CopyFile copyFile=new CopyFile(sourceFile,targetFile,blockCount,i);
            //实例化线程
            Thread thread=new Thread(copyFile);
            //开始线程
            thread.start();
            try
            {
                //加入线程
                thread.join();
            }
            catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        //计算耗时
        long endTime=System.currentTimeMillis();
        //输出耗时
        System.out.println("共用时:"+(endTime-beginTime)+"ms");

    }
}

上面那段代码转载字`这里 , 标叔 具体出处不详。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章