题目
完整代码:
package IDCard;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Ic {
public static String get18Ic(String identifyCard) {
String retId = "";
String id17 = "";
int sum = 0;
int y = 0;
int[] wf = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
String[] cc = { "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2" };
id17 = identifyCard.substring(0, 6) + "19" + identifyCard.substring(6);
for (int i = 0; i < 17; i++) {
sum = sum + Integer.valueOf(id17.substring(i, i + 1)) * wf[i];
}
y = sum % 11;
retId = id17 + cc[y];
return retId;
}
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader=new BufferedReader(new FileReader(file));
String tempString=null;
int line=1;
while((tempString=reader.readLine())!=null){
String returnId=get18Ic(tempString);
WriterFile(returnId);
line++;
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
static String file=null;
public static void WriterFile(String returnID){
FileWriter fw = null ;
try {
File f=new File(file);
fw=new FileWriter(f,true);
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter pw=new PrintWriter(fw);
pw.println(returnID+"\n");
pw.flush();
try {
fw.flush();
pw.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String FileName(){
SimpleDateFormat simpleDateFormat;
simpleDateFormat =new SimpleDateFormat("yyyyMMddHHmmss");
Date date=new Date();
String filenametxt=simpleDateFormat.format(date);
return filenametxt;
}
public static void main(String[] args) {
file="C:\\Users\\李强\\Desktop\\IDCardNo("+FileName()+").txt";
System.out.println("正在读取文件,转换中");
readFileByLines("C:\\Users\\李强\\Desktop\\15.txt");
System.out.println("转换完成!");
}
}