java大文件分割成多个文件

大文件分割

/**
     *
     * @param filePath      文件路径(不包括格式后缀)
     * @param fileFormat    文件后缀格式(如:.txt)
     * @param fileCount     分割文件数量
     * @param fileOverSize  文件超过多大才分割(单位:M)
     * @throws IOException
     */
    public static void splitFile(String filePath,String fileFormat, int fileCount ,int fileOverSize) throws IOException {
        FileInputStream fis = new FileInputStream(filePath+fileFormat);
        FileChannel inputChannel = fis.getChannel();
        final long fileSize = inputChannel.size();
        if (fileSize < fileOverSize*1024*1024)  return;
        long average = fileSize / fileCount;//平均值
        long bufferSize = 200; //缓存块大小,自行调整
        ByteBuffer byteBuffer = ByteBuffer.allocate(Integer.valueOf(bufferSize + "")); // 申请一个缓存区
        long startPosition = 0; //子文件开始位置
        long endPosition = average < bufferSize ? 0 : average - bufferSize;//子文件结束位置
        for (int i = 0; i < fileCount; i++) {
            if (i + 1 != fileCount) {
                int read = inputChannel.read(byteBuffer, endPosition);// 读取数据
                readW:
                while (read != -1) {
                    byteBuffer.flip();//切换读模式
                    byte[] array = byteBuffer.array();
                    for (int j = 0; j < array.length; j++) {
                        byte b = array[j];
                        if (b == 10 || b == 13) { //判断\n\r
                            endPosition += j;
                            break readW;
                        }
                    }
                    endPosition += bufferSize;
                    byteBuffer.clear(); //重置缓存块指针
                    read = inputChannel.read(byteBuffer, endPosition);
                }
            }else{
                endPosition = fileSize; //最后一个文件直接指向文件末尾
            }

            int m = i+1;
            while(new File(filePath+m+fileFormat).exists()){
                m++;
            }
            FileOutputStream fos = new FileOutputStream(filePath + m + fileFormat);
            FileChannel outputChannel = fos.getChannel();
            inputChannel.transferTo(startPosition, endPosition - startPosition, outputChannel);//通道传输文件数据
            outputChannel.close();
            fos.close();
            startPosition = endPosition + 1;
            endPosition += average;
        }
        inputChannel.close();
        fis.close();
        Resource.writeFile("",notifyFileName+notifyFileFormat,false); //分割完后源文件置空
    }

文本写入

 /**
     *
     * @param data      写入文本内容
     * @param fileName  文件名
     * @param append    是否追加写入
     */
    public static void writeFile(String data,String fileName,Boolean append) {
        try {
            File file = new File(Environment.getExternalStorageDirectory(),fileName); // 相对路径,如果没有则要建立一个新的output.txt文件
            if (!file.exists()) {
                file.createNewFile();
            }
            try (FileWriter writer = new FileWriter(file,append);
                 BufferedWriter out = new BufferedWriter(writer)
            ) {
                if (data.equals("")){
                    out.write(data);
                }else{
                    out.write(data+"\r\n");// \r\n即为换行
                }
                out.flush();
                out.close();
                writer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(java大文件分割成多个文件)