package com.test;

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.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dao.BaseDao;

public class BatchPhone extends BaseDao {
	public static void main(String[] args) {
//				String file = "F:\\test_ad";
//				readPhoneNdSave(file);
		String file = "F:\\025.txt";
		readFile(new File(file));
	}

	/**
	 *   读文件夹下面的文件
	 * @param filePath
	 */
	public static void readPhoneNdSave(String filePath) {
		File dir = new File(filePath); // 拿到文件夹
		File[] flist = dir.listFiles(); // 拿到文件夹下的所有文件
		int i;
		FileInputStream fis = null;
		InputStreamReader isr;
		BufferedReader br;
		int sum = 0;
		String ts = "";
		try {
			BaseDao.getConn().setAutoCommit(false);
			pstmt = BaseDao.getConn().prepareStatement(
					"insert into TASK_TEST_PHONE(phone,time,status) values(?,sysdate,0)");
			for (i = 0; i < flist.length; i++) // 枚举,flist.length就是该文件夹下文件的个数。
			{
				if (flist[i].getName().endsWith(".txt"))
				// 判是否是java源文件
				{
					try {
						fis = new FileInputStream(flist[i]);// 打开文件流
						isr = new InputStreamReader(fis);// 打开读入流
						br = new BufferedReader(isr);// 用缓存流包装
						while (true) // 如果流中还有数据就继续读,相当于枚举所有行
						{
							ts = br.readLine(); // 每次读一行
							if (ts == null) // 如果为空表示读到文件尾
								break; //
							// if (ts.equals("") == false){} // 判空行,空行不累计
							if (!ts.equals("") && ts.length() == 11) {
								pstmt.setString(1, ts);
								pstmt.addBatch();
								sum++;// 计数器+1
								if (sum % 1000 == 0) {
									pstmt.executeBatch();
								}
							}
						}
						pstmt.executeBatch();
						BaseDao.getConn().commit();
					} catch (FileNotFoundException e) {// 处理异常
						System.out.println("fileNotFound");
					} catch (Exception e) { // 处理异常
						System.out.println("异常");
					} finally {
						try {
							fis.close();// 最终关闭文件流
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(sum);// 输出
	}
	
	public static void readFile(File file){
		FileInputStream fis = null;
		InputStreamReader isr;
		BufferedReader br;
		int sum = 0;
		String ts = null;
		try {
			BaseDao.getConn().setAutoCommit(false);
			pstmt = BaseDao.getConn().prepareStatement(
					"insert into test_phone_025(id,phone) values(?,?)");
			fis = new FileInputStream(file);// 打开文件流
			isr = new InputStreamReader(fis);// 打开读入流
			br = new BufferedReader(isr);// 用缓存流包装
			while (true) // 如果流中还有数据就继续读,相当于枚举所有行
			{
				ts = br.readLine(); // 每次读一行
				if (ts == null) // 如果为空表示读到文件尾
					break; //
				// if (ts.equals("") == false){} // 判空行,空行不累计
				if (!ts.equals("")) {
					sum++;// 计数器+1
					pstmt.setInt(1, sum);
					pstmt.setString(2,ts);
					pstmt.addBatch();
					if (sum % 1000 == 0) {
						pstmt.executeBatch();
					}
				}
			}
			pstmt.executeBatch();
			BaseDao.getConn().commit();
		} catch (FileNotFoundException e) {// 处理异常
			System.out.println("fileNotFound");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println(sum);// 输出
	}

	@SuppressWarnings("static-access")
	public static List<String> getPhone(String sql) {
		List<String> list = new ArrayList<String>();
		try {
			ResultSet rs = new BaseDao().getConn().prepareStatement(sql)
					.executeQuery();
			while (rs.next()) {
				list.add(rs.getString("phone"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
	public String setStr(String str){
		return null;
	}
}
最近下载更多
3199625134  LV10 2022年5月19日
1524757  LV2 2021年3月19日
929100703  LV3 2020年6月8日
pengyouqian  LV1 2020年5月19日
liudeshuai970926  LV6 2020年3月18日
15838634741  LV18 2019年9月19日
wangmeicong  LV12 2019年1月30日
大果冻  LV2 2018年9月4日
子不语语不子  LV11 2018年8月14日
dachoumomo  LV12 2018年6月12日
最近浏览更多
微信网友_6411774242443264 2023年11月27日
暂无贡献等级
cxdxfx12  LV14 2022年6月21日
3199625134  LV10 2022年5月19日
2021年12月15日
暂无贡献等级
Dimoo1 2021年11月29日
暂无贡献等级
yiersansiwuliu  LV4 2021年7月5日
业务管理  LV7 2021年4月27日
1524757  LV2 2021年3月19日
nihao123456  LV6 2021年1月12日
newhaijun  LV15 2020年12月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友