按行分割文件脚本

1.Java脚本

  

package com.wind.tdap;

import java.io.*;

/**
 *
 * 文件切割脚本
 * Created by huangyongjie on 2018/9/3.
 */
public class TestSplitFile {
    //需要切割的文件路径+名称
    private static final String filepath = "D:\\Desktop\\bi\\filename";

    //文件的类型
    private static final String filetype = ".txt";

    //按多少行切割
    private static final int rowTotal = 800000;

    //换行符
    private static final String enter = "\r\n";

    public static void main(String[] args) {
        File file = new File(filepath+filetype);//要切割的文件
        BufferedWriter output = null;
        BufferedReader reader = null;
        String rowString = null;
        int line =1;//行号
        int filenum = 0;//文件号
        try {
            reader = new BufferedReader(new FileReader(file));
            File wf = new File(filepath + filenum +filetype); //切割之后的文件
            output = new BufferedWriter(new FileWriter(wf,true));//true,则追加写入text文本
            while ((rowString = reader.readLine()) != null) {
                output.write(rowString);
                line ++ ;
                if(line%rowTotal==0){
                    output.flush();
                    output.close();
                    wf = new File(filepath + filenum + filetype);
                    output = new BufferedWriter(new FileWriter(wf,true));//true,则追加写入text文本
                    filenum++;
                }else{
                    output.write(enter);//换行
                }
            }
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(output!=null){
                try{
                    output.flush();
                    output.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }


}

2.shell脚本

  linenum=`wc   -l   filename.txt|   awk   '{print   $1}'`   
  n1=1   
  file=1   
  while   [   $n1   -lt   $linenum   ]   
  do   
                  n2=`expr   $n1   +   9999`   
                  sed   -n   "${n1},   ${n2}p"   filename.txt >   filename_$file.txt     
                  n1=`expr   $n2   +   1`   
                  file=`expr   $file   +   1`   
  done   

windows上编写的需要在上传linux服务器之后转码,:set ff=unix

3.linux上面可以用split命令切分文件,但是需要安装插件。

 

 

 

 

 

你可能感兴趣的:(工具脚本)