2017.08.16-IO练习

来源:互联网 发布:c语言while e 编辑:程序博客网 时间:2024/05/24 05:32
package com.qianfeng.homework;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.Closeable;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.text.SimpleDateFormat;import java.util.Date;import com.qianfeng.demo02.FileUtils;//1,模拟两人聊天,当一方说出"bye",退出系统!//把聊天信息,聊天时间,时间保存到chat.txt中;public class Homework01 {public static void main(String[] args) {//1.选择流BufferedReader br = null;BufferedWriter bw = null;//接收控制台输入的流br = new BufferedReader(new InputStreamReader(System.in));try {bw = new BufferedWriter(new FileWriter("chat.txt"));SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String line = null;String time = null;while (true) {//生成随机数,确定是由谁说的。  0,1int random = (int)(Math.random()*2);if (random==1) {System.out.println("小红说:");//接受控制台输入的信息line = br.readLine();time = sdf.format(new Date());bw.write(time);bw.newLine();bw.write("小红说:"+line);bw.newLine();}else{System.out.println("小明说:");//接受控制台输入的信息line = br.readLine();time = sdf.format(new Date());bw.write(time);bw.newLine();bw.write("小明说:"+line);bw.newLine();}if (line.equalsIgnoreCase("bye")) {break;}bw.flush();}System.out.println("退出聊天!!");} catch (IOException e) {e.printStackTrace();}finally {FileUtils.closeIO(new Closeable[]{br,bw});}}}
package com.qianfeng.homework;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Scanner;import com.qianfeng.demo02.FileUtils;public class Homework02 {static List<WorldCup>list = new ArrayList<>();public static void main(String[] args) {readFileInfo();Scanner sc = new Scanner(System.in);outer:while (true) {System.out.println("您是要查询年份1,举办地2,还是冠军国3,退出请输入0:");int select = sc.nextInt();switch (select) {case 0:System.out.println("感谢您的使用,正在退出系统!!");break outer;case 1:System.out.println("请输入年份:");String year = sc.next();boolean b1 = false;for (WorldCup wCup : list) {if (wCup.getYear().equals(year)) {b1 = true;System.out.println(wCup);}}if (!b1) {System.out.println("此年份没有举办过世界杯,请重新查询!!");}break;case 2:System.out.println("请输入举办地:");String country = sc.next();boolean b2 = false;for (WorldCup wCup : list) {if (wCup.getCountry().equals(country)) {System.out.println(wCup);b2 = true;}if (country.equals("韩国")|country.equals("日本")) {if (wCup.getCountry().contains(country)) {System.out.println(wCup);b2 = true;}}}if (!b2) {System.out.println("此国家没有举办过世界杯,请重新输入!!");}break;case 3:System.out.println("请输入冠军国:");String winner = sc.next();boolean b3 = false;for (WorldCup cup : list) {if (cup.getWinner().equals(winner)) {b3 = true;System.out.println(cup);}}if (!b3) {System.out.println("这个国家没有获得过大力神杯,还需继续努力!!");}break;default:break;}}}public static void readFileInfo(){BufferedReader br = null;try {br = new BufferedReader(new FileReader("country.txt"));String line = null;while (true) {line = br.readLine();if (line.equals("over")) {break;}String[] arr = line.split("\\|");WorldCup wc = new WorldCup(arr[0], arr[1], arr[2]);list.add(wc);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {FileUtils.closeIO(br);}}}
package com.qianfeng.homework;public class WorldCup {private String year;private String country;private String winner;public String getYear() {return year;}public void setYear(String year) {this.year = year;}public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}public String getWinner() {return winner;}public void setWinner(String winner) {this.winner = winner;}public WorldCup(String year, String country, String winner) {super();this.year = year;this.country = country;this.winner = winner;}public WorldCup() {super();}@Overridepublic String toString() {return "年份:" + year + ", 举办地:" + country + ", 冠军国:" + winner;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((country == null) ? 0 : country.hashCode());result = prime * result + ((winner == null) ? 0 : winner.hashCode());result = prime * result + ((year == null) ? 0 : year.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;WorldCup other = (WorldCup) obj;if (country == null) {if (other.country != null)return false;} else if (!country.equals(other.country))return false;if (winner == null) {if (other.winner != null)return false;} else if (!winner.equals(other.winner))return false;if (year == null) {if (other.year != null)return false;} else if (!year.equals(other.year))return false;return true;}}
package com.qianfeng.homework;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FilenameFilter;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import com.qianfeng.demo02.FileUtils;/*【1】请使用File类在当前项目下创建目录qianfeng,在该目录下又有三个文件夹分别是Android、IOS、Html5, * 在Html5文件夹下创建文件msg.txt(5分)【2】有一字符串String msg="千锋互联是一家专业做移动互联网教育培训的!千锋互联在全国很多城市都设有分校!千锋互联先开设课程有Android、IOS、HTML5!千锋互联助你圆高薪梦想!加入我们,千锋互联将引领你走向IT巅峰";,请统计"千锋互联"出现的次数并将次数和当前时间打印在该项目下qianfeng文件夹中Count.txt(3分)【3】将上述字符串按照”!”分割成若干字符串,将各个字符串分别写入到当前项目的msg1.txt,msg2.txt.......文件中,并在qianfeng\Html5\msg.txt中写入:信息添加成功(4分)【4】将上述写的msg1.txt,msg2.txt......文件剪切到qianfeng\Android目录下(4分)*/public class Homework03 {public static void main(String[] args) {//createFile();//writeFile();//writeMsgToFile();renameFile();}//第四题public static void renameFile(){File file = new File("");file = file.getAbsoluteFile();File[] listFiles = file.listFiles(new FilenameFilter() {@Overridepublic boolean accept(File dir, String name) {if (name.startsWith("msg")&name.endsWith(".txt")) {return true;}return false;}});for (File childFile : listFiles) {childFile.renameTo(new File("qianfeng\\Android", childFile.getName()));}}//第三题public static void writeMsgToFile(){String msg = "千锋互联是一家专业做移动互联网教育培训的!千锋互联在全国很多城市都设有分校!千锋互联先开设课程有Android、IOS、HTML5!千锋互联助你圆高薪梦想!加入我们,千锋互联将引领你走向IT巅峰";String[] split = msg.split("!");FileOutputStream fos = null;for (int i = 0; i < split.length; i++) {try {fos = new FileOutputStream("msg"+(i+1)+".txt");fos.write(split[i].getBytes());fos.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {FileUtils.closeIO(fos);}}try {fos = new FileOutputStream("qianfeng\\Html5\\msg.txt");fos.write("信息添加成功".getBytes());fos.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {FileUtils.closeIO(fos);}}//第二题public static void writeFile(){String msg = "千锋互联是一家专业做移动互联网教育培训的!千锋互联在全国很多城市都设有分校!千锋互联先开设课程有Android、IOS、HTML5!千锋互联助你圆高薪梦想!加入我们,千锋互联将引领你走向IT巅峰";String[] ss = msg.split("!");int count = 0;for (String line : ss) {if (line.contains("千锋互联")) {count++;}}SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time = sdf.format(new Date());FileOutputStream fos = null;try {fos = new FileOutputStream("qianfeng\\Count.txt");fos.write((count+"次\n").getBytes());fos.write(time.getBytes());fos.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {FileUtils.closeIO(fos);}}//第一题:public static void createFile(){File file = new File("qianfeng");file.mkdirs();String filename[] = {"Android","IOS","Html5"};File childFile = null;for (String name : filename) {childFile = new File(file, name);childFile.mkdirs();if (name.equals("Html5")) {File f = new File(childFile, "msg.txt");try {f.createNewFile();} catch (IOException e) {e.printStackTrace();}}}}}




原创粉丝点击