按行分割文件脚本

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();
                    filenum++;
                    wf = new File(filepath + filenum + filetype);
                    output = new BufferedWriter(new FileWriter(wf,true));//true,则追加写入text文本
                    
                }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命令切分文件,但是需要安装插件。

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值