nio实现文件读取写入数据库或文件
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();
- }
- }
- }
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();
}
}
}
最新文章
- hdu 4717(三分求极值)
- MySQL的几个概念:主键,外键,索引,唯一索引
- mybatis错误之配置文件属性配置问题
- pair<;>;结构体模版的用法
- vmware screen
- AX2012全新的批处理方式
- 十六进制字符串转化成字符串输出HexToStr(Delphi版、C#版)
- 用 .htaccess 实现网址规范化
- Linux使用SSH远程连接方式和更改密码 ,自己空间转移过来的。
- 数据库中的记录通过servlet回显到jsp页面中(连接数据库或者查询參照:对数据进行增删改查)
- parseInt(),parseFloat(),parse()
- sql对每一条记录都给他一个随机的数。
- linux下tomcat 8的安装以及tomcat启动慢问题
- net core EF 链接mysql 数据库
- Linux(Ubuntu 16) 下Java开发环境的配置(二)------Tomcat的配置及常见问题
- Servlet和Filter的区别
- Gradle 依赖管理
- python 画个小猪佩奇
- Form的is_valid校验规则及验证顺序
- Helm
热门文章
- android -- 小问题 关于ListView设置了OnScrollListener之后onScrollStateChanged()和onScroll方法监听不到的问题
- 纯CSS实现小圆点和三角形图案
- nginx 1.5 支持websocket
- UVA 1415 - Gauss Prime(数论,高斯素数拓展)
- poj--1789--Truck History(prim)
- 【DNN】 制作一个扩展程序
- dedecms实现表单提交数据到指定的邮箱
- coedforces #481Div(3)(ABCDEFG)
- SPOJ8222 NSUBSTR - Substrings 后缀自动机_动态规划
- oracle基础入门(四)