Java文件按行数切割分文件

由于在工作遇到了大文件无法导入公司的响应系统,就写了简单文件分割的方法来处理,日常记录一下,有什么不妥的地方希望同行留言指教。

/**  
 * @Title: FileCatTool.java
 * @Package com.lincomb.tool
 * @Description: 
 * TXT文件切割
 * @author lin.xu@lincomb.com
 * @date 2018年4月17日
 * @version V1.0  
 */

    
package com.lincomb.tool;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @ClassName: FileCatTool
 * @Description: 
 * TXT文件切割  按照行数读取  进行分割
 * @author lin.xu@lincomb.com
 * @date 2018年4月17日
 *
 */
public class FileCatTool {
    //字符集
    private static String Encoding = "UTF-8";
    //切割行数
    private static int CAT_LONG_NUM = 6000;
    //换行符
    private static String SPLIT_CHAR = "\n";
    private static String W_SPLIT_CHAR = "\r\n";
    
    
    public static void main(String[] args) {
        long statTime = System.currentTimeMillis();
        String filePath = "C:/Users/Administrator/Desktop/导数据文件/";
        String fileName = "周边游";
        StringBuffer bf = readTxt(filePath + fileName +".txt");
        catStrInfo(bf,fileName,filePath);
        long endTime = System.currentTimeMillis();
        System.out.println("共耗时:" + (endTime - statTime));
    }
    
    
    /**
     * @Title: readTxt
     * @Description: 
     * 读取文件中的信息到缓存中
     * @param path
     * @return
     * StringBuffer 返回值
     * @author lin.xu@lincomb.com
     * @throws
     */
    @SuppressWarnings("resource")
    public static StringBuffer readTxt(String path){
        StringBuffer stringBuffer = new StringBuffer();
        RandomAccessFile raf;
        try {
            File f = new File(path);
            raf = new RandomAccessFile(f, "r");
            byte[]  buff = new byte[(int)raf.length()];
            //用于保存实际读取的字节数
            int hasRead=0;
            //循环读取
            while ((hasRead=raf.read(buff)) > 0 ) {
                //打印读取的内容,并将字节转为字符串输入
                stringBuffer.append(new String(buff,0,hasRead,Encoding));
            }
            return stringBuffer;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuffer;
    }
    
    /**
     * @Title: catStrInfo
     * @Description: 
     * 进行文件流分割  6000为一个文件
     * @param str
     * void 返回值
     * @author lin.xu@lincomb.com
     * @throws
     */
    public static void catStrInfo(StringBuffer str,String fileName,String filePath){
        if(null != str){
            String[] rStrArry = str.toString().split(SPLIT_CHAR);
            if(null != rStrArry && rStrArry.length > 0){
                System.out.println("读取文件中的数据行数:" + rStrArry.length);
                StringBuffer wrSb = new StringBuffer();
                int n = 0;
                for(int i = 1 ; i <= rStrArry.length ; i++){
                    wrSb.append(rStrArry[i-1]).append(W_SPLIT_CHAR);
                    //当数据长度最大的时候进行开启线程
                    if(i == rStrArry.length){
                        StringBuffer goWrSb = wrSb;
                        //启动线程
                        WriteFileThread t = new WriteFileThread(goWrSb,n,fileName,filePath);
                        t.start();
                        //清空数据长度
                        wrSb = new StringBuffer();
                        n++;
                    }
                    //是倍数时进行开启线程处理
                    if(i%CAT_LONG_NUM == 0){
                        System.out.println("开启线程" + n);
                        StringBuffer goWrSb = wrSb;
                        //启动线程
                        WriteFileThread t = new WriteFileThread(goWrSb,n,fileName,filePath);
                        t.start();
                        //清空数据长度
                        wrSb = new StringBuffer();
                        n++;
                    }
                }
            }
        }
    }
    
    
}

/**
 * @ClassName: writeFileThread
 * @Description: 
 * 执行写文件
 * @author lin.xu@lincomb.com
 * @date 2018年4月17日
 */
class WriteFileThread extends Thread{
    //写入字符
    private StringBuffer wsbstr;
    //文件序号
    private int num;
    //文件名
    private String fileName;
    //文件路径
    private String filePath;
    
    public WriteFileThread(StringBuffer wsbstr, int num, String fileName,String filePath){
        this.wsbstr = wsbstr;
        this.num = num;
        this.fileName = fileName;
        this.filePath = filePath;
    }
    
    /* (非 Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */ 
    @Override
    public void run() {
        try {
            File wf = new File(filePath+fileName+"_"+num+".txt");
            if(!wf.exists()){
                wf.createNewFile();
            }
            RandomAccessFile rFile = new RandomAccessFile(wf, "rw");
            byte[] b = wsbstr.toString().getBytes();
            long originLen = wf.length();
            rFile.setLength(originLen + b.length);
            rFile.seek(originLen);
            rFile.write(b);
            rFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}

评论

暂无评论