时间:2021-07-01 10:21:17 帮助过:27人阅读
package com.ally;import java.io.File;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import java.sql.*;/** * Created by admin on 2016/6/28. * 1.nio分批读取sql文件并执行插入数据库 * 2.读取一个文件写入另外文件 */public class TestNio { public static void main(String args[]) throws Exception { System.err.println("begin"); long start = System.currentTimeMillis(); int _5M = 1024 * 1024 * 5; File fin = new File("D:\\drug_general_info.sql"); FileChannel fcin = new RandomAccessFile(fin, "r").getChannel(); ByteBuffer rBuffer = ByteBuffer.allocate(_5M); //将文件读取执行到数据库 readFileByLine(_5M, fcin, rBuffer); //将文件读取并写入另外文件 File fout = new File("D:\\mm.sql"); FileChannel fcout = new RandomAccessFile(fout, "rws").getChannel(); ByteBuffer wBuffer = ByteBuffer.allocateDirect(_5M); saveOtherFile( _5M, fcin, rBuffer, fcout, wBuffer); System.err.print((System.currentTimeMillis() - start) / 1000); } /** * 将一个文件内容写入另外一个 * @param bufSize * @param fcin * @param rBuffer * @param fcout * @param wBuffer */ public static void saveOtherFile(int bufSize, FileChannel fcin, ByteBuffer rBuffer, FileChannel fcout, ByteBuffer wBuffer){ Statement pst = null; String enterStr = "\n"; try { byte[] bs = new byte[bufSize]; StringBuilder strBuf = new StringBuilder(""); String tempString = null; while (fcin.read(rBuffer) != -1) { int rSize = rBuffer.position(); rBuffer.rewind(); rBuffer.get(bs); rBuffer.clear(); tempString = new String(bs, 0, rSize); int fromIndex = 0; int endIndex = 0; int i = 0; while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) { String line = tempString.substring(fromIndex, endIndex); line = strBuf.toString() + line; writeFileByLine(fcout, wBuffer, line); strBuf.delete(0, strBuf.length()); fromIndex = endIndex + 1; } } } catch (Exception e) { e.printStackTrace(); } } /** * 读文件写入数据库 * @param bufSize * @param fcin * @param rBuffer */ public static void readFileByLine(int bufSize, FileChannel fcin, ByteBuffer rBuffer) { Connection conn = null; Statement pst = null; String enterStr = "\n"; try { byte[] bs = new byte[bufSize]; StringBuilder strBuf = new StringBuilder(""); String tempString = null; Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/doctor?useUnicode=true&characterEncoding=utf-8", "root", "root"); pst = conn.createStatement(); while (fcin.read(rBuffer) != -1) { int rSize = rBuffer.position(); rBuffer.rewind(); rBuffer.get(bs); rBuffer.clear(); tempString = new String(bs, 0, rSize); int fromIndex = 0; int endIndex = 0; int i = 0; while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) { String line = tempString.substring(fromIndex, endIndex); line = strBuf.toString() + line; strBuf.delete(0, strBuf.length()); fromIndex = endIndex + 1; pst.addBatch(line); /* System.out.println("-----------------------"); System.out.println(line); System.out.println("-----------------------");*/ if (i % 100 == 0) { System.out.println("执行了:" + i); if (i == 2700) { System.out.println("导了:" + i); } int[] flag = pst.executeBatch(); System.out.println("结果:" + flag[0]); } i += 1; } // 执行批量更新 pst.executeBatch(); if (rSize > tempString.length()) { strBuf.append(tempString.substring(fromIndex, tempString.length())); } else { strBuf.append(tempString.substring(fromIndex, rSize)); } // System.out.println(strBuf.toString()); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (pst != null) { pst.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } public static void writeFileByLine(FileChannel fcout, ByteBuffer wBuffer, String line) { try { fcout.write(wBuffer.wrap(line.getBytes()), fcout.size()); } catch (Exception e) { e.printStackTrace(); } }}
nio实现文件读取写入数据库或文件
标签:one mac 读取 package about on() data err null