转换前的格式:1894297,716,荆州
转换后的格式:insert into TA_NUMBER(AREA_ID, NUMBERTYPE, NUM)values('0662', 'CDMA-189', '861890080');
import java.io.*;
public class TxtReader {
public static void main(String[] args)
{
String s;
s=calculate();
System.out.println(s);
}
static String calculate()
{
StringBuffer sb=new StringBuffer("");
String desFile = "C:\\fileio.txt";
//目标文件---需要写入的文件
try{
FileReader reader = new FileReader("C:"+File.separator+"189.txt");
BufferedReader br = new BufferedReader(reader);
PrintWriter out = new PrintWriter(
new FileWriter(
new File(desFile)));
String line = null;
while((line = br.readLine()) != null) {
String[] strs = line.split(",");
byte[] b = strs[0].getBytes();
String tmp;
if(b[1]=='8'&&b[2]=='9'){
tmp="CDMA-189";
}else if(b[1]=='5'&&b[2]=='3'){
tmp="CDMA-153";
}else if(b[1]=='3'&&b[2]=='3'){
tmp="CDMA-133";
}else if(b[1]=='8'&&b[2]=='0'){
tmp="CDMA-180";
}else{
tmp="err";
}
String area ="0"+strs[1];
String route ="86"+strs[0];
sb.append("insert into TA_NUMBER(AREA_ID, NUMBERTYPE, NUM)values('"+area+
"','"+tmp+"','"+route+"');" +'\n');
out.write("insert into TA_NUMBER(AREA_ID, NUMBERTYPE, NUM)values('"+area+
"','"+tmp+"','"+route+"');" +"\r\n");
}
out.flush();
out.close();
br.close();
reader.close();
}catch(Exception e){
e.printStackTrace();
}
return sb.toString();
}
}
本文介绍了一种使用Java实现的批量转换电话号码格式的方法。通过读取文本文件中的原始数据,根据号码前两位进行类型判断,并转换为指定的SQL插入语句格式。此方法能够高效地处理大量号码数据。

被折叠的 条评论
为什么被折叠?



