1.读txt文件
package com.review;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ReadFilePractice {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// File file = new File("C:/Users/xqbigbear/Desktop/test.txt");
// FileInputStream fis = new FileInputStream(file);
// InputStreamReader isr = new InputStreamReader(fis, "gbk");
// BufferedReader br = new BufferedReader(isr);
/**
* 装饰者模式简写
*/
InputStreamReader is = new InputStreamReader(new FileInputStream(new File("C:/Users/xqbigbear/Desktop/test.txt")),"GBK");
// 如果直接对is使用read方法,效率不高,故将is用BufferedReader包装起来,每次缓冲一部分再读
BufferedReader br = new BufferedReader(is);
String s;
long start = System.currentTimeMillis();
while((s=br.readLine())!=null){
System.out.println((String)s);
}
long end = System.currentTimeMillis();
is.close();
br.close();
System.out.println("");
System.out.println("耗时 " + (end-start) + " ms");
}
}
2.写入txt
package com.review;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class WriteFilePractice {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// FileOutputStream fo = new FileOutputStream(new File("C:/Users/xqbigbear/Desktop/new.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(new File("C:/Users/xqbigbear/Desktop/new.txt")), "GBK");
BufferedWriter bw = new BufferedWriter(osw);
String s = "这和FileReader并没有什么区别,事实上在FileReader";
bw.write(s);
bw.flush();
osw.close();
}
}
3.读并写入数据库
package com.test.readTxt;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class ReadText {
public static void main(String[] args) throws IOException, Exception {
// TODO Auto-generated method stub
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@202.114.3.5:1521:YQDB", "YQDB_USER_HU",
"xwdb");
conn.setAutoCommit(false);
PreparedStatement pst = conn
.prepareStatement("insert into YQ_SENSITIVITY_KEY values(yq_seq_SENSITIVITY_KEY.nextval, ?,10,'seqing')");
InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("C:/Users/xqbigbear/Desktop/敏感词汇大全 绝对够用/敏感词库/2012年最新敏感词列表/领导名人类.txt")), "gb2312");
BufferedReader read = new BufferedReader(isr);
String s = null;
int i = 1;
long start = System.currentTimeMillis();
while ((s = read.readLine()) != null) {
// 当数据满100条批量插入
if (i % 100 == 0) {
// 语句执行完毕,提交本事务
pst.executeBatch();
// 此处的事务回滚是必须的,网上很多代码没有处理,会导致插入数据不完整。
try {
conn.commit();
} catch (Exception e) {
conn.rollback();
}
} else {
s=s.substring(0,s.indexOf('='));
pst.setString(1, s);
pst.addBatch();
}
System.err.println(s);
i++;
}
// 由于每次达到100条插入,如果数据不为100的倍数的话,最后一次会剩下一些。下面的代码正好处理剩下的数据
// 语句执行完毕,提交本事务
pst.executeBatch();
try {
conn.commit();
} catch (Exception e) {
conn.rollback();
}
long end = System.currentTimeMillis();
// 此处是打印插入效果
System.out.println("插入" + i + "条,耗时" + (end - start) + "ms");
read.close();
}
}