nio实现文件读取写入数据库或文件
来源:互联网 发布:php 00 截断原理 编辑:程序博客网 时间:2024/05/04 18:07
1.nio实现读取大文件,之后分批读取写入数据库
2.nio实现读取大文件,之后分批写入指定文件
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(); } }}
0 0
- nio实现文件读取写入数据库或文件
- Java NIO 读取文件、写入文件、读取写入混合
- Java NIO 读取文件、写入文件、读取写入混合
- nio(三)--文件读取和写入
- 读取文件并写入数据库
- Java文件读取,写入数据库
- Java 一次性读取或写入文件内容
- java一行一行写入或读取文件
- Java读取或写入Excel文件
- php实现将文件写入数据库、从数据库读取文件(防止图片上传漏洞)
- php如何读取文件并写入数据库
- c#大文件读取和写入数据库
- 从数据库中读取和写入文件
- C# 读取TXT文件并写入数据库
- C#大文件读取和写入数据库
- PHP读取EXCEL文件写入数据库
- PHP读取TXT文件写入数据库
- Java读取Excel文件并写入数据库
- PreparedStatement
- Python 字典中的中文输出问题
- Linux 硬件篇
- PostgresQL FDW 源码分析之 GetForeignRelSize()
- angularJS缓存的管理
- nio实现文件读取写入数据库或文件
- leetcode 38. count and say
- 【转】JavaScript 中的变量和函数提升
- mybatis association 复杂类型联合操作
- 二叉树------和为定值的全部路径
- hdu 1166
- hdu4925Apple Tree【水】2014多校联合
- 1.Centos创建及联网(VirtualBox)
- jQuery EasyUI使用教程之树的拖放控制