java 中用字符流读取和写入文件,涉及到根据给定正则表达式的匹配拆分此字符串(split)

时间:2022-01-14 14:42:12

public static void main(String[] args) {
getChaXun();
}

/**
* @param year 输入的年
* @param c Counyry对象
* @return 日志中追加的后缀
*/
public static String Panduan(String year,Country c) {
String string=null;
if (year.equals("1")) {
string="进行了“查询年份------"+c.getNianfen();
}else if (year.equals("2")) {
string="进行了“查询举办地------"+c.getJubanji();
}else if (year.equals("3")) {
string="进行了“查询冠军国------"+c.getHuojiangguojia();
}
return string;
}

/**
* 开始查询
*/
public static void getChaXun() {
Scanner scanner=new Scanner(System.in);
System.out.println("您是要:1.查询年份 2.查询举办地 3. 查询冠军国 0.退出");
System.out.println("请输入查询方式:");
String year1=scanner.next();
BufferedReader reader=null;
BufferedWriter writ=null;
StringBuffer sb=null;
Country country=null;
try {
reader=new BufferedReader(new FileReader("message.txt"));
writ=new BufferedWriter(new FileWriter("查询记录.log",true));
String read = null;
String str=" 年份 举办地 冠军国 查询时间";
writ.write(str+"\n");
writ.write("---------------------------------------------------------------------------------------------------------------------------------------"+"\n");
if (year1.compareTo("0")!=0) {
System.out.println("请输入年份、举办地、或者是冠军国:");
String year=scanner.next();
while ((read=reader.readLine())!=null) {
String[] split = read.split("\\|");
// System.out.println(split.length);
if (read.indexOf("|")>0) {
country=new Country( split[0], split[1], split[2]);
switch (year1) {
case "1":
if (year.equals(split[0])) {
System.out.println("举办地:"+country.getJubanji()+";获奖国家:"+country.getHuojiangguojia());
String panduan = Panduan(year1, country);
String string=country.getNianfen()+" "+country.getJubanji()+" "
+country.getHuojiangguojia()+" "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+
panduan+
"\n";
writ.write(string);

//list.add(country);
}
break;
case "2":
if (year.equals(split[1])) {
System.out.println(country);
String panduan = Panduan(year1, country);
String string=country.getNianfen()+" "+country.getJubanji()+" "
+country.getHuojiangguojia()+" "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+
panduan+
"\n";
writ.write(string);

//list.add(country);
}
break;
case "3":
if (year.equals(split[2])) {
System.out.println(country);
String panduan = Panduan(year1, country);
String string=country.getNianfen()+" "+country.getJubanji()+" "
+country.getHuojiangguojia()+" "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+
panduan+
"\n";
writ.write(string);


//list.add(country);
}
break;

default:
break;
}

}else{
System.out.println("谢谢查询");
}

}

} else {
System.out.println("已退出");
}

writ.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (null!=reader) {
scanner.close();
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


public class Country {
/*年份*/
protected String nianfen;
/*举办地*/
protected String jubanji;
/*获奖国家*/
protected String huojiangguojia;



public Country(String nianfen, String jubanji, String huojiangguojia) {
super();
this.nianfen = nianfen;
this.jubanji = jubanji;
this.huojiangguojia = huojiangguojia;
}
public String getNianfen() {
return nianfen;
}
public void setNianfen(String nianfen) {
this.nianfen = nianfen;
}
public String getJubanji() {
return jubanji;
}
public void setJubanji(String jubanji) {
this.jubanji = jubanji;
}
public String getHuojiangguojia() {
return huojiangguojia;
}
public void setHuojiangguojia(String huojiangguojia) {
this.huojiangguojia = huojiangguojia;
}
@Override
public String toString() {
return " " + nianfen + " " +jubanji + " " + huojiangguojia +" "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}

}