由于在工作遇到了大文件无法导入公司的响应系统,就写了简单文件分割的方法来处理,日常记录一下,有什么不妥的地方希望同行留言指教。
1/** 2 * @Title: FileCatTool.java 3 * @Package com.lincomb.tool 4 * @Description: 5 * TXT文件切割 6 * @author lin.xu@lincomb.com 7 * @date 2018年4月17日 8 * @version V1.0 9 */ 10 11 12package com.lincomb.tool; 13 14import java.io.File; 15import java.io.FileNotFoundException; 16import java.io.IOException; 17import java.io.RandomAccessFile; 18 19/** 20 * @ClassName: FileCatTool 21 * @Description: 22 * TXT文件切割 按照行数读取 进行分割 23 * @author lin.xu@lincomb.com 24 * @date 2018年4月17日 25 * 26 */ 27public class FileCatTool { 28 //字符集 29 private static String Encoding = "UTF-8"; 30 //切割行数 31 private static int CAT_LONG_NUM = 6000; 32 //换行符 33 private static String SPLIT_CHAR = "\n"; 34 private static String W_SPLIT_CHAR = "\r\n"; 35 36 37 public static void main(String[] args) { 38 long statTime = System.currentTimeMillis(); 39 String filePath = "C:/Users/Administrator/Desktop/导数据文件/"; 40 String fileName = "周边游"; 41 StringBuffer bf = readTxt(filePath + fileName +".txt"); 42 catStrInfo(bf,fileName,filePath); 43 long endTime = System.currentTimeMillis(); 44 System.out.println("共耗时:" + (endTime - statTime)); 45 } 46 47 48 /** 49 * @Title: readTxt 50 * @Description: 51 * 读取文件中的信息到缓存中 52 * @param path 53 * @return 54 * StringBuffer 返回值 55 * @author lin.xu@lincomb.com 56 * @throws 57 */ 58 @SuppressWarnings("resource") 59 public static StringBuffer readTxt(String path){ 60 StringBuffer stringBuffer = new StringBuffer(); 61 RandomAccessFile raf; 62 try { 63 File f = new File(path); 64 raf = new RandomAccessFile(f, "r"); 65 byte[] buff = new byte[(int)raf.length()]; 66 //用于保存实际读取的字节数 67 int hasRead=0; 68 //循环读取 69 while ((hasRead=raf.read(buff)) > 0 ) { 70 //打印读取的内容,并将字节转为字符串输入 71 stringBuffer.append(new String(buff,0,hasRead,Encoding)); 72 } 73 return stringBuffer; 74 } catch (FileNotFoundException e) { 75 e.printStackTrace(); 76 } catch (IOException e) { 77 e.printStackTrace(); 78 } 79 return stringBuffer; 80 } 81 82 /** 83 * @Title: catStrInfo 84 * @Description: 85 * 进行文件流分割 6000为一个文件 86 * @param str 87 * void 返回值 88 * @author lin.xu@lincomb.com 89 * @throws 90 */ 91 public static void catStrInfo(StringBuffer str,String fileName,String filePath){ 92 if(null != str){ 93 String[] rStrArry = str.toString().split(SPLIT_CHAR); 94 if(null != rStrArry && rStrArry.length > 0){ 95 System.out.println("读取文件中的数据行数:" + rStrArry.length); 96 StringBuffer wrSb = new StringBuffer(); 97 int n = 0; 98 for(int i = 1 ; i <= rStrArry.length ; i++){ 99 wrSb.append(rStrArry[i-1]).append(W_SPLIT_CHAR); 100 //当数据长度最大的时候进行开启线程 101 if(i == rStrArry.length){ 102 StringBuffer goWrSb = wrSb; 103 //启动线程 104 WriteFileThread t = new WriteFileThread(goWrSb,n,fileName,filePath); 105 t.start(); 106 //清空数据长度 107 wrSb = new StringBuffer(); 108 n++; 109 } 110 //是倍数时进行开启线程处理 111 if(i%CAT_LONG_NUM == 0){ 112 System.out.println("开启线程" + n); 113 StringBuffer goWrSb = wrSb; 114 //启动线程 115 WriteFileThread t = new WriteFileThread(goWrSb,n,fileName,filePath); 116 t.start(); 117 //清空数据长度 118 wrSb = new StringBuffer(); 119 n++; 120 } 121 } 122 } 123 } 124 } 125 126 127} 128 129/** 130 * @ClassName: writeFileThread 131 * @Description: 132 * 执行写文件 133 * @author lin.xu@lincomb.com 134 * @date 2018年4月17日 135 */ 136class WriteFileThread extends Thread{ 137 //写入字符 138 private StringBuffer wsbstr; 139 //文件序号 140 private int num; 141 //文件名 142 private String fileName; 143 //文件路径 144 private String filePath; 145 146 public WriteFileThread(StringBuffer wsbstr, int num, String fileName,String filePath){ 147 this.wsbstr = wsbstr; 148 this.num = num; 149 this.fileName = fileName; 150 this.filePath = filePath; 151 } 152 153 /* (非 Javadoc) 154 * 155 * @see java.lang.Runnable#run() 156 */ 157 @Override 158 public void run() { 159 try { 160 File wf = new File(filePath+fileName+"_"+num+".txt"); 161 if(!wf.exists()){ 162 wf.createNewFile(); 163 } 164 RandomAccessFile rFile = new RandomAccessFile(wf, "rw"); 165 byte[] b = wsbstr.toString().getBytes(); 166 long originLen = wf.length(); 167 rFile.setLength(originLen + b.length); 168 rFile.seek(originLen); 169 rFile.write(b); 170 rFile.close(); 171 } catch (IOException e) { 172 e.printStackTrace(); 173 } 174 } 175 176}