mysql將查詢部分數據導出到外部文件的腳本:
mysql -u root -p222222 -e "use ccbdj2014;SELECT A.CERT_NO 證件號碼,A.CUST_NAME 客戶姓名,A.MOBEL 聯繫電話,MOBEL_NUM 交易筆數 FROM CUST_INFO A WHERE CHANEL='02' ORDER BY A.MOBEL_NUM;">/tmp/ccbdj.txt
說明:root:用戶名,222222:密碼,ccbdj2014:數據庫名稱 , >/tmp/ccbdj.txt:導出到tmp下的ccbdj文件中
注意:腳本文件編碼應該與數據庫編碼格式一致!
生成文件格式:證件號碼客戶姓名 聯繫電話交易筆數
將文件格式替換爲:證件號碼,客戶姓名,聯繫電話,交易筆數 格式代碼如下:
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
*
* @author xlhu
* CCBDJ2013生成全量數據、部分數據,數據格式爲:證件號碼,客戶姓名,聯繫電話,交易筆數
*
*/
public class FileTest {
/**
* 全量數據
* @throws Exception
*/
private static void runAll() throws Exception{
File file = new File("F:/feiqiu文件/ccbjd.txt");
String date = "0303";
File foutPath = new File("F:\\Work\\CCBDJ2013\\2014每週數據\\"+date);
if(!foutPath.exists()){
foutPath.mkdir();
}
File fout = new File(foutPath,"全量數據"+date+".txt");
if(!fout.exists()){
fout.createNewFile();
}
String strLine = null;
if(!file.exists()){
System.out.println("源文件不存在!");
return;
}else{
String lineRead = null;
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fout),"GBK"));
long allLine = 1;
while((lineRead = br.readLine()) != null){
strLine = lineRead.replaceAll(" ", ",");
strLine = strLine+"\n";
bw.write(strLine);
allLine++;
}
bw.close();
br.close();
System.out.println("總計行數:"+allLine);
}
}
/**
* 全量數據
* @throws Exception
*/
private static void runSome() throws Exception{
File file = new File("F:/feiqiu文件/ccbjd.txt");
String date = "0303";
File foutPath = new File("F:\\Work\\CCBDJ2013\\2014每週數據\\"+date);
if(!foutPath.exists()){
foutPath.mkdir();
}
File fout = new File(foutPath,"部分數據"+date+".txt");
if(!fout.exists()){
fout.createNewFile();
}
String strLine = null;
if(!file.exists()){
System.out.println("源文件不存在!");
return;
}else{
String lineRead = null;
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fout),"GBK"));
long allLine = 1;
while((lineRead = br.readLine()) != null){
strLine = lineRead.replaceAll(" ", ",");
if(strLine.charAt(strLine.length()-1) == '0'){
continue;
}else{
strLine = strLine+"\n";
bw.write(strLine);
allLine++;
}
}
bw.close();
br.close();
System.out.println("總計行數:"+allLine);
}
}
public static void main(String[] args) {
try {
//全量數據
runAll();
//部分數據
runSome();
} catch (Exception e) {
e.printStackTrace();
}
}
}