龙空技术网

java实现对文件进行切割

从事java的小白 889

前言:

现时看官们对“java文件切分”都比较珍视,我们都想要分析一些“java文件切分”的相关资讯。那么小编在网摘上搜集了一些关于“java文件切分””的相关知识,希望朋友们能喜欢,咱们一起来了解一下吧!

一、创建相应的工具类

public class SplitFileUtil {    public static final String _encoding = "GB2312";    /**     *     * @param sourceFileUrl 源路径     * @param targetPath 目标路径     * @return     * @throws IOException     */    public static List<String> separateFileByLine(String sourceFileUrl,String targetPath) throws IOException {        List<String> filenames = new ArrayList<>();        int file_count;        //获取文件总行数        long lineNum = getFileLineNum(sourceFileUrl);        //不做分文件处理 Constants.line_num_per_file;自定义的常量        if(lineNum<= Constants.line_num_per_file){            filenames.add(sourceFileUrl);        }else{            //分割文件            if(lineNum%Constants.line_num_per_file==0){                file_count = (int)(lineNum/Constants.line_num_per_file);            }else{                file_count = (int)(lineNum/Constants.line_num_per_file+1);            }            Long[] countArray;            if(file_count>Constants.max_file_count){                file_count = Constants.max_file_count;                //将每个文件的起始索引记录下来                countArray  = new Long[file_count];                long file_start_index;                int line_num_per_file;                if(lineNum%file_count==0){                    line_num_per_file = (int)(lineNum/file_count);                }else{                    line_num_per_file = (int)(lineNum/file_count+1);                }                for(int i=0;i<file_count;i++){                    file_start_index = (i+1)*line_num_per_file;                    countArray[i] = file_start_index;                }            }else{                //将每个文件的起始索引记录下来                countArray  = new Long[file_count];                long file_start_index = 0;                for(int i=0;i<file_count;i++){                    file_start_index = (i+1)*Constants.line_num_per_file;                    countArray[i] = file_start_index;                }            }         /*处理文件*/            InputStreamReader fr = null;            BufferedReader br = null;            try {                fr = new InputStreamReader(new FileInputStream(sourceFileUrl),_encoding);                br = new BufferedReader(fr);                int count = 0;                int _index = 0;                String rec ;// 一行                List<String> result = new ArrayList<>();                //读取掉第一行数据,第一行为标题                br.readLine();                // 读取一行                while ((rec = br.readLine()) != null) {                    count++;                    result.add(rec);                    if(count==countArray[_index]){                        //一次性写入文件                        String targetFile  = targetPath+"data_part_"+_index+".csv";                        writeCsv(targetFile,result);                        result.clear();                        _index++;                        filenames.add(targetFile);                    }                }                if(result.size()>0){                    String targetFile  = targetPath+"data_part_"+(file_count-1)+".csv";                    writeCsv(targetFile,result);                    result.clear();                    filenames.add(targetFile);                }            }catch(Exception e){                e.printStackTrace();            }finally {                if(!StringUtils.isEmpty(br)){                    br.close();                }                if(!StringUtils.isEmpty(fr)){                    fr.close();                }            }        }        return filenames;    }    public static void writeCsv(String csvFile,List<String> contentList) throws IOException {        BufferedWriter bw = null;        try {            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), _encoding), 1024);            for(int i=0;i<contentList.size();i++){                String str = contentList.get(i);                bw.write(str);                bw.newLine();            }            bw.flush();        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally{            if(!StringUtils.isEmpty(bw)){                bw.close();            }        }    } public static long getFileLineNum(String filename) throws IOException {        long time1 = System.currentTimeMillis();        //取出文件总行数        long count = 0;        InputStreamReader fr = null;        BufferedReader br = null;        try {            fr = new InputStreamReader(new FileInputStream(filename),_encoding);            br = new BufferedReader(fr);            // 读取一行            while (br.readLine() != null) {                count++;            }        }catch(Exception e){            e.printStackTrace();        }finally {            if(!StringUtils.isEmpty(br)){                br.close();            }            if(!StringUtils.isEmpty(fr)){                fr.close();            }        }        long time2 = System.currentTimeMillis();        System.out.println("getFileLineNum==["+count+"] time is:"+(time2-time1));        return count;    }}

实现写入文件的方法

获取文件的行数

标签: #java文件切分