java mysql导入txt文件_java 读取文件夹并存入数据库(TXT)

package com.zte.m2m.dao.upload;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPFile;

import org.apache.commons.net.ftp.FTPReply;

import java.sql.CallableStatement;

import java.sql.Connection;

import oracle.sql.ARRAY;

import com.zte.ismp.common.dao.CommonDao;

import util.DBHandle;

/**

*

* 描述信息:数据批量汇入

*

* @author xieyuchun

* @version 1.0

*

* 完成日期:2014-06-13

*

*/

public class DataBatchUploadDao extends CommonDao {

/*

* 文件入库

*

* filePath:文件夹绝对路径

*/

public void upload (String filePath){

System.out.println("文件入库");

DBHandle handler = null;

Connection con = null;

CallableStatement cstmt = null;

try{

//数据库连接对象

handler = this.getHandler();

//获取数据库连接

con = handler.getCon();

if(1 == 1){

String encoding="GBK";

File folder = new File(filePath);

//folder.list() 获取文件夹下所有文件名

String[] fileArray = folder.list();

for (String fileName : fileArray) {

System.out.println(fileName);

File file = new File(filePath+"\\"+fileName);

fileName = fileName.substring(0, fileName.indexOf("."));

// //判断文件是否存在

if (file.isFile() && file.exists())

{

InputStreamReader read = new InputStreamReader(

new FileInputStream(file), encoding);

BufferedReader bufferedReader = new BufferedReader(read);

String lineTxt = null;

// 读取txt

while ((lineTxt = bufferedReader.readLine()) != null) {

System.out.println(lineTxt);

cstmt = con.prepareCall("{ call pro_forall_insert(?,?,?) }");

cstmt.setObject(1,fileName);

cstmt.setObject(2, lineTxt);

cstmt.setObject(3, ",");

cstmt.execute();

}

read.close();

System.out.println("文件入库完成");

} else {

System.out.println("找不到指定的文件");

}

}

}

} catch (Exception e) {

throw new RuntimeException(e);

} finally {

if (null != handler) {

handler.release();

}

}

}

}


版权声明:本文为weixin_39637975原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。