java的大文件切割-工具类;
场景使用:超大文件sql 的导入导出操作~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
public static void main(String[] args){ int m=1000; File src = new File("G:/zfsoa/zfsoa.sql"); if(src.isFile()) { //获取文件的总长度 long l = src.length(); //获取文件名 String fileName = src.getName().substring(0, src.getName().indexOf(".")); //获取文件后缀 String endName = src.getName().substring(src.getName().lastIndexOf(".")); System.out.println(endName); InputStream in = null; try { in = new FileInputStream(src); for(int i = 1; i <= m; i++) { StringBuffer sb = new StringBuffer(); sb.append(src.getParent()).append("\\").append(fileName) .append("_data").append(i).append(endName); System.out.println(sb.toString()); File file2 = new File(sb.toString()); //创建写文件的输出流 OutputStream out = new FileOutputStream(file2); int len = -1; byte[] bytes = new byte[10*1024*1024]; while((len = in.read(bytes))!=-1) { out.write(bytes, 0, len); if(file2.length() > (l / m)) { break; } } out.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } |