txt导入

package timer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

import db.DbConn;

public class Test {
public static DbConn DB = new DbConn();
public static void main(String[] args) throws Exception {
int num = 5;
int i = 0;
int b = 0;
int j = 0;
chinese ch = new chinese();
try {
String aa = "d:/c.txt";
String date = "";
int a;
Connection con = DB.getConnection();
File file = new File(aa);
InputStreamReader read = new InputStreamReader(new FileInputStream(file),"unicode");
BufferedReader br = new BufferedReader(read);
String sql = "insert into c(a) values(?)";
PreparedStatement pre = null;
pre = con.prepareStatement(sql);
List list = new ArrayList();
while(( date =br.readLine())!= null){
System.out.println(date);
list.add(date);
}
int total = list.size();
while (i < total) {
for (; i < list.size(); i++) {
String d = list.get(i).toString();
pre.setString(1, d);
pre.addBatch();
j++;
if (j % num == 0) {
pre.executeBatch();
pre.clearParameters();
pre.clearBatch();
b = total - j;
}
}
if (b < num) {
System.out.println(b);
pre.executeBatch();
}
}
pre.close();
System.out.println("over-----");
con.close();
br.close();
read.close();
} catch (FileNotFoundException e) {
System.out.println("找不到文件.....");
} catch (IOException ioe) {
System.out.println("IO异常.....");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("不能插入......");
System.out.println(ex.getMessage());
System.out.println(ex.getStackTrace());
System.out.println(ex.getLocalizedMessage());
}

}
}


-------------------------------------------
package timer;
//主要用来转换成汉字 防止出现乱码
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class chinese {
public List chinesezhuanhuan(String file)
{
List list = new ArrayList();
try
{
InputStream r = new FileInputStream (file);
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
byte tmp [] = new byte [1024];
byte context [];
int i = 0 ;
while ((i = r.read(tmp)) !=- 1 )
{
byteout.write(tmp);
}
context = byteout.toByteArray();
String str = new String(context,"unicode" );
// 分隔行
String stra [] = str.split( "\n" );
for ( int n = 0 ;n < stra.length;n ++ )
{
list.add(stra[n]);
}
System.out.println(list.size());
} catch (Exception e)
{
e.printStackTrace();
list = null;
}
return list;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值