身份证读取
读取文件&信息元素切割
FileInputStream fileInputStream = new FileInputStream(inputPath);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
BufferedReader reader = new BufferedReader(inputStreamReader);
String tempString = null;
while((tempString = reader.readLine()) != null){
String[] items = tempString.split(", ");
String str = items[9];
读取文件&信息元素切割
public static String IdCheck(String idstr){
if(idstr.length()<16)
{
return " , , ";
}
String id=idstr.substring(1, idstr.length()-2);
System.out.println(id);
//身份证号码中对应的出生年月日及长度和省份
int year,month,day,lent,province,sex;
//用户通过Dialog框输入身份证号
//判断输入的号码长度是否合法,如果长度不合法,警告提示
//从ID中获取省份、出生年月日、性别
province=Integer.parseInt(id.substring(0,2));
year=Integer.parseInt(id.substring(6,10));
month=Integer.parseInt(id.substring(10,11));
day=Integer.parseInt(id.substring(12,13));
sex=Integer.parseInt(id.substring(16));
String sexAttr = "";
//定义数组array用于各月份最大天数的合法判断
int array[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
String array2[]={"0","0","0","0","0","0","0","0","0","0","0",
"北京","天津","河北","山西","内蒙古","0","0","0","0","0",
"辽宁","吉林","黑龙江","0","0","0","0","0","0","0",
"上海","江苏","浙江","安徽","福建","江西","山东","0","0","0",
"河南","湖北","湖南","广东","广西","海南","0","0","0",
"重庆","四川","贵州","云南","西藏","0","0","0","0","0","0",
"陕西","甘肃","青海","宁夏","新疆"
};
//定义数组array用于各月份最大天数的合法判断
/*
//判断省份是否在合法范围内
if (province<=00||province>=83)
System.out.println("号码有误!");
//判断出生年份是否在合法范围内
else if (year<1900||year>=2012)
System.out.println("号码年份有误!");
//判断出生月份是否在合法范围内
else if (month<1||month>12)
System.out.println("号码月份有误!");
//判断闰年时二月份情况,闰年二月份最高不能超过29天
//ID在合法范围内
else{
if (year %400==0||(year%4==0&&year%100!=0)&&month==2)
{
if (day>29||day<=0)
System.out.println("号码日期有误!");
}
//根据上面定义的数组判断非闰年情况下各月份生日是否合法
else if (array[month]<=day||day<=0){
System.out.println("码出生日期有误!");
return null;
}
*/
int age = year - Calendar.YEAR;
//System.out.println();
if(month >Calendar.MONTH)
age=age+1;
ageAttr = age;
//System.out.println(ageAttr);
if(sex%2==0){
sexAttr = "女";
}
else{
sexAttr = "男";
}
return array2[province]+", "+ ageAttr +", "+sexAttr;
}
将新生成数据贴在原数组前面
public static String AppendColomn(String[] str,String appendString) {
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!");
for (String elementA:str ) {
System.out.print(elementA + " ");
}
//增加ruby
ArrayList<String> list = new ArrayList<String>();
list.add(appendString);
System.out.println("appendString");
for (int i=0; i<str.length; i++) {
list.add(str[i]);
System.out.println(str[i]);
}
String c="";
for(int i = 0;i<list.size();i++){
c = c + list.get(i)+ ", ";
}
System.out.println();
System.out.println(c);
return c;
}
写文件操作
public static void WriteFile(String str, String outputPath) throws Exception{
try{
FileOutputStream os = new FileOutputStream(outputPath, true); //true是append设为允许,即可以在原文件末端追加。
//将字节流转换成字符流
OutputStreamWriter osw = new OutputStreamWriter(os);
//把接收到的字符流放入缓冲区,提高读写速度。
BufferedWriter bw = new BufferedWriter(osw);
bw.write(str);
bw.newLine();
//将字符串以流的形式写入文件
bw.close();
osw.close();
os.close();
}catch(FileNotFoundException e){
System.out.println("找不到指定文件");
}catch(IOException e){
System.out.println("写入文件错误");
}
}
清理数据,只保留带有想要信息的数据
public static void ExtractUsefulInfo(String inputString,String outputString){
try {
// read file content from file
StringBuffer sb= new StringBuffer("");
FileReader reader = new FileReader(inputString);
BufferedReader br = new BufferedReader(reader);
String str = null;
String tempString = "";
while((str = br.readLine())!= null) {
if(str.contains("男")||str.contains("女")){
// write string to file
FileOutputStream os = new FileOutputStream(outputString, true); //true是append设为允许,即可以在原文件末端追加。
//将字节流转换成字符流
OutputStreamWriter osw = new OutputStreamWriter(os);
//把接收到的字符流放入缓冲区,提高读写速度。
BufferedWriter bw = new BufferedWriter(osw);
bw.write(str);
bw.newLine();
//将字符串以流的形式写入文件
bw.close();
osw.close();
os.close();
System.out.println(str);
}
}
System.out.println("@@@@@@@@@@@" + tempString);
br.close();
reader.close();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
截取文件:取第N行开始的Txt文档进行重写
public static void CutTextFile(String inputString,String outputString){
try {
System.out.println("woyaokaishiqiele!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// read file content from file
StringBuffer sb= new StringBuffer("");
FileReader reader = new FileReader(inputString);
BufferedReader br = new BufferedReader(reader);
String str = null;
int rows = 0;
String tempOutputString = "";
while((str = br.readLine()) != null) {
rows++;
if(rows<(countLines-9)){
continue;
}
else{
sb.append(str+"/n");
tempOutputString+=(str+"\r\n");
}
System.out.println(str);
}
br.close();
reader.close();
// write string to file
FileOutputStream os = new FileOutputStream(outputString, true); //true是append设为允许,即可以在原文件末端追加。
//将字节流转换成字符流
OutputStreamWriter osw = new OutputStreamWriter(os);
//把接收到的字符流放入缓冲区,提高读写速度。
BufferedWriter bw = new BufferedWriter(osw);
bw.write("var nianlingbi = [");
bw.newLine();
bw.write(tempOutputString);
bw.write("]");
//将字符串以流的形式写入文件
bw.close();
osw.close();
os.close();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
读取文件的行数
public static void CalculateLines(){
try {
FileReader fr = new FileReader(newFile);
BufferedReader br = new BufferedReader(fr);
while(br.readLine()!=null){
countLines++;
}
br.close(); //随手关流
fr.close(); //随手关流
} catch (IOException e) {}
}
年龄结构统计
public static String StatisticAge(String str){
System.out.println(str);
String[] items = str.split(", ");
int age;
if(items[1].contains(" ")){
age = 66;
}
else age = 2016 - Integer.parseInt(items[1]);
System.out.println(age);
if(age<=15) age10++;
else if(age>15&&age<=20) age15++;
else if(age>20&&age<=25) age20++;
else if(age>25&&age<=30) age25++;
else if(age>30&&age<=35) age30++;
else if(age>35&&age<=40) age35++;
else if(age>40&&age<=45) age40++;
else if(age>45&&age<=50) age45++;
else if(age>50&&age<=55) age50++;
else if(age>55&&age<=65)age55++;
String ageS10 = "0-15";
String ageS15 = "15-20";
String ageS20 = "20-25";
String ageS25 = "25-30";
String ageS30 = "30-35";
String ageS35 = "35-40";
String ageS40 = "40-45";
String ageS45 = "45-50";
String ageS50 = "50-55";
String ageS55 = "55+";
String ageStatistic =
"{AgeRange:\""+ageS10 + "\", number:" +age10+"},"+"\r\n"
+"{AgeRange:\""+ageS15 + "\", number:" +age15+"},"+"\r\n"
+"{AgeRange:\""+ageS20 + "\", number:" +age20+"},"+"\r\n"
+"{AgeRange:\""+ageS25 + "\", number:" +age25+"},"+"\r\n"
+"{AgeRange:\""+ageS30 + "\", number:" +age30+"},"+"\r\n"
+"{AgeRange:\""+ageS35 + "\", number:" +age35+"},"+"\r\n"
+"{AgeRange:\""+ageS40 + "\", number:" +age40+"},"+"\r\n"
+"{AgeRange:\""+ageS45 + "\", number:" +age45+"},"+"\r\n"
+"{AgeRange:\""+ageS50 + "\", number:" +age50+"},"+"\r\n"
+"{AgeRange:\""+ageS55 + "\", number:" +age55+"},"+"\r\n";
return ageStatistic;
}
性别结构统计
public static void CountGender(String str){
System.out.println(str);
String[] items = str.split(", ");
for (String elementA:items ) {
System.out.print(elementA + " ");
}
if(items[2].contains("男")){
male++;
System.out.println("男人变多了");
System.out.println(male+"------"+female);
}
else if(items[2].contains("女")){
System.out.println("女人变多了");
female++;
System.out.println(male+"--------"+female);
}
else{ System.out.println(male+"---"+female);
System.out.println("无效");}
// float maleRate = male / (male+female);
// return maleRate;
}