博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 中用字符流读取和写入文件,涉及到根据给定正则表达式的匹配拆分此字符串(split)...
阅读量:5098 次
发布时间:2019-06-13

本文共 3774 字,大约阅读时间需要 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());
}
}

转载于:https://www.cnblogs.com/2070393244com/p/8967115.html

你可能感兴趣的文章
lc 145. Binary Tree Postorder Traversal
查看>>
在centos上开关tomcat
查看>>
android dialog使用自定义布局 设置窗体大小位置
查看>>
ionic2+ 基础
查看>>
查询消除重复行
查看>>
[leetcode]Minimum Path Sum
查看>>
内存管理 浅析 内存管理/内存优化技巧
查看>>
Aizu - 1378 Secret of Chocolate Poles (DP)
查看>>
csv HTTP简单表服务器
查看>>
IO流写出到本地 D盘demoIO.txt 文本中
查看>>
Screening technology proved cost effective deal
查看>>
mysql8.0.13下载与安装图文教程
查看>>
Thrift Expected protocol id ffffff82 but got 0
查看>>
【2.2】创建博客文章模型
查看>>
Kotlin动态图
查看>>
从零开始系列之vue全家桶(1)安装前期准备nodejs+cnpm+webpack+vue-cli+vue-router
查看>>
Jsp抓取页面内容
查看>>
大三上学期软件工程作业之点餐系统(网页版)的一些心得
查看>>
可选参数的函数还可以这样设计!
查看>>
[你必须知道的.NET]第二十一回:认识全面的null
查看>>