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();
}
}
}
nio实现文件读取写入数据库或文件的更多相关文章
- PostgreSql那点事(文件读取写入、命令执行的办法)
• 2013/07/9 作者: admin PostgreSql那点事(文件读取写入.命令执行的办法) 今天无意发现了个PostgreSQL环境,线上学习了下,一般的数据注射(读写数据库)差异不大,不 ...
- xml文件读取到数据库
xml文件读取到数据库 第一步,导包 c3p0,dom4j,jaxen,MySQL-connector 第二步 xml文件,config文件 第三步 javabean 第四步 c3p0的工具类 ...
- Excel 读取写入数据库
// Excel 读取写入数据库 // 3.8版本的poi 4.0 可以不用写 parseCell 这个方法,可以直接赋值 STRING 类型 import org.apache.poi.hss ...
- SpringBoot读取Linux服务器某路径下文件\读取项目Resource下文件
// SpringBoot读取Linux服务器某路径下文件 public String messageToRouted() { File file = null; try { file = Resou ...
- (OAF)jdeveloper集成log4j并将日志输出到指定文件并写入数据库
参考: How to configure Log4j in JDev 11g Ever wanted to use log4j in your adf project ? Well though Or ...
- python向config、ini文件读取写入
config读取操作 cf = configparser.ConfigParser() # 实例化对象 cf.read(filename) # 读取文件 cf.sections() # 读取secti ...
- html外部文件读取/写入
1.文件的读取 外部文件读取控件: <input type="file" id="file_jquery" onchange="file_jqu ...
- python学习笔记6-输入输出与文件读取写入
(1)打印到屏幕:print (2)读取键盘输入:input/raw_input #键盘输入 str = raw_input("Please enter:"); print (&q ...
- shell cat 合并文件,合并数据库sql文件
> 覆盖写入 >> append模式写入 ###################################################################合并数 ...
随机推荐
- SDUTOJ 2776 小P的故事——奇妙的分组
#include<iostream> #include<math.h> #include<memory.h> using namespace std; int dp ...
- django 笔记7 多对多
多对多 方法一 :双外键关联 自定义关系表 自定义 class Host(models.Model): nid = models.AutoField(primary_key=True) hostnam ...
- [luogu P3360] 偷天换日 解题报告(树形DP)
题目链接:https://www.luogu.org/problemnew/show/P3360 题解: 首先我们把边上的消耗放到向下的点上,如果是叶子节点的话就先做一次0/1背包 发现这是一颗二叉树 ...
- Java 类和对象3
编写Java应用程序.首先,定义描述学生的类——Student,包括学号(int).姓名(String).年龄(int)等属性:二个方法:Student(int stuNo,String name,i ...
- AngularJs轻松入门源码托管至Github
Github是全球最大的代码托管平台,笔者玩Github有一段时间了,有很多开源项目的源码都托管在Github上,笔者在上面也发现了不少优秀的开源代码. 每次写完博文想在最后附上文章相关的代码,但是由 ...
- windows下MySQL5.6以上版本,如何通过修改配置文件来修改数据库的最大连接数啊?
并没有my.ini文件,只有一个my-default.ini文件,并且里面并没有max_connections windows下MySQL5.6以上版本,如何通过修改配置文件来修改数据库的最大连接数啊 ...
- exsi从磁盘中加载虚拟机
e
- 禁用cache
Z:\src\services\network\network_context.cc:http_cache_enabled
- vue下载模板、导出excle
1.下载模板是 下载模版比较简单,就是跳一个新的页面 2.导出excle 就是get请求,把自己要导出的参数一一拼接起来 }
- php文件加载、错误处理、方法函数和数组
数组运算符注意:php中,数组的元素的顺序,不是由下标(键名)决定的,而是完全由加入的顺序来决定.联合(+):将右边的数组项合并到左边数组的后面,得到一个新数组.如有重复键,则结果以左边的为准$v1 ...