在文件传输过程中,经常需要将一个文件拆分成多个较小的文件,然后利用多线程传输这些小文件,最后再对这些小文件进行合并。这里先给出文件拆分的一个demo,稍后将会给出文件合并的介绍。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hfut.wst.io;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 *
 * @author 风停心止
 */
public class RandomAccessFileDemo {

    public static void main(String[] args) {
        try {
            RandomAccessFile file = new RandomAccessFile("randomFile1.txt", "rws");//创建一个随即读写文件
            String[] names = {"zhangsan is me\r\n", "lisi is my deskmate\r\n", "wangwu is my best friend\r\n"};//待写入内容
            String[] chineseName = {"张三\r\n", "李四r\n", "王五\r\n"};//待写入内容
            for (String name : names) {//写入英文内容
                file.seek(file.length());//将文件指针定位在文件结尾,实现追加功能
                byte[] nameByte = name.getBytes();
                file.write(nameByte);//写入内容
            }
            for (String name : chineseName) {//写入中文内容
                file.seek(file.length());
                byte[] nameByte = name.getBytes();
                file.write(nameByte);
            }
            System.out.println(file.length());
            file.seek(0);//将文件指针置为文件开始
            String test = "";
            while ((test = file.readLine()) != null) {
                String line = new String(test.getBytes("ISO-8859-1"));//读取文件内容并正确处理乱码和编码问题
                System.out.println(line);
            }
//            int seekIndex = 0;
//            byte[] b = new byte[256];
//            while (seekIndex <= file.length()) {
//                file.seek(seekIndex);
//                file.read(b);
//                System.out.println(b);
//                seekIndex += b.length;
//            }
            int blockSize = 256;//每个文件块的大小
            int blockNum = (int) file.length() / blockSize + 1;//计算文件可以拆分的数目
            File[] blocks = new File[blockNum];//将拆分小文件放在某一数组中
            byte[] block = new byte[blockSize];//定义内容数组
            file.seek(0);//从文件头开始拆分
            int fileIndex = 1;
            int index = 0;
            while (file.getFilePointer() < file.length()) {
                String path = "block" + fileIndex + ".txt";
                File temp = new File(path);//创建小文件
                file.read(block);//读取blockSize字节内容到block中
                DataOutputStream dos = new DataOutputStream(new FileOutputStream(temp));//创建小文件输出流
                dos.write(block);//向小文件中写入拆分内容
                blocks[index] = temp;//继续进行拆分
                fileIndex++;
                index++;
                //System.out.println(path + " is created");
                dos.close();//关闭输入流,否则下面将无法删除小文件
            }
            System.out.println(block.length);
            for (File f : blocks) {//删除小文件
                if (f.exists()) {
                    System.out.println(f.getAbsoluteFile());//显示拆分小文件路径
                    if (!f.delete()) {
                        System.out.println("请关闭使用该文件的所有进程或者流!!");
                    } else {
                        System.out.println(f.getName()+" 成功被删除!");
                    }
                }
            }
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}