1、Java面向对象程序设计实验实验八输入输出流广西科技大学计通学院Java面向对象程序设计实验实验八 输入输出流 学生: 学 号: 班 级: 指导老师: 专 业:计算机科学与技术提交日期:年 月日实验报告容1实验目的掌握字符输入、输出流用法;掌握使用Scanner类解析文件;掌握Console流的使用。2实验容实验教材-第12章实验1、2、3字符输入输出流;Scanner类和Console类。 要求: 完善程序,给出实验结果截图;完成试验后练习。3程序代码及运行结果:实验1 举重成绩单/ AnalysisResult.javaimport java.io.*; import java.util.
2、*;public class AnalysisResult public static void main(String args) File fRead=new File(score.txt); File fWrite=new File(scoreAnalysis.txt); try Writer out=new FileWriter(fWrite); BufferedWriter bufferWrite=new BufferedWriter(out); Reader in=new FileReader(fRead); BufferedReader bufferRead=new Buffer
3、edReader(in); String str=null; while(str=bufferRead.readLine()!=null) double totalScore=Fenxi.getTotalScore(str); str=str+总成绩:+totalScore; System.out.println(str); bufferWrite.write(str); bufferWrite.newLine(); bufferRead.close(); bufferWrite.close(); catch(IOException e) System.out.println(e.toStri
4、ng(); / Fenxi.javaimport java.util.*; public class Fenxi public static double getTotalScore(String s) String regex = 0123456789.; String digitMess = s.replaceAll(regex,*); StringTokenizer fenxi = new StringTokenizer(digitMess,*); double totalScore=0; while(fenxi.hasMoreTokens() double score=Double.p
5、arseDouble(fenxi.nextToken(); totalScore=totalScore+score; return totalScore; / score.txt:三, 抓举成绩 106kg, 挺举 189kg。:四, 抓举成绩 108kg, 挺举 186kg。:周五, 抓举成绩 112kg, 挺举 190kg。运行结果如图1所示图1实验2 统计英文单词/ WordStatistic.javaimport java.io.*; import java.util.*;public class WordStatistic Vector allWord,noSameWord; Fil
6、e file=new File(english.txt); Scanner sc=null; String regex; WordStatistic () allWord=new Vector(); noSameWord=new Vector(); regex=sdpPunct+;/正则表达式 try sc=new Scanner(file); sc.useDelimiter(regex); catch(IOException exp) System.out.println(exp.toString(); void setFileName(String name) file=new File(
7、name); try sc=new Scanner(file); sc.useDelimiter(regex); catch(IOException exp) System.out.println(exp.toString(); void WordStatistic() try while(sc.hasNext() String word=sc.next(); allWord.add(word); if(!noSameWord.contains(word) noSameWord.add(word); catch(Exception e) public VectorgetAllWord() re
8、turn allWord; public VectorgetNoSameWord() return noSameWord; / OutputWordMess.javaimport java.util.*;public class OutputWordMess public static void main(String args) Vector allWord,noSameWord; WordStatistic statistic=new WordStatistic(); statistic.setFileName(hello.txt); statistic.WordStatistic();
9、allWord=statistic.getAllWord(); noSameWord=statistic.getNoSameWord(); System.out.println(共有+allWord.size()+个英文单词); System.out.println(有+noSameWord.size()+个互不相同英文单词); System.out.println(按出现的频率排列:); int count=new intnoSameWord.size(); for(int i=0;inoSameWord.size();i+) String s1=noSameWord.elementAt(i
10、); for(int j=0;jallWord.size();j+) String s2=allWord.elementAt(j); if(s1.equals(s2) counti+; for(int m=0;mnoSameWord.size();m+) for(int n=m+1;ncountm) String temp=noSameWord.elementAt(m); noSameWord.setElementAt(noSameWord.elementAt(n), m); noSameWord.setElementAt(temp, n); int t=countm; countm=coun
11、tn; countn=t; for(int m=0;mnoSameWord.size();m+) double frequency=(1.0*countm)/allWord.size(); System.out.printf(%s:%-7.3f, noSameWord.elementAt(m),frequency); 运行结果如图2所示图2实验3 密码流/ PassWord.javaimport java.io.*;public class PassWord public static void main(String args) boolean success=false; int coun
12、t=0; Console cons; char passwd; cons=System.console(); while(true) System.out.print(输入密码:); passwd=cons.readPassword(); count+; String password= new String(passwd); if(password.equals(tiger123) success=true; System.out.println(您第+count+次输入的密码正确!); break; else System.out.println(您第+count+次输入的密码+passw
13、ord+不正确!); if(count=3) System.out.println(您+count+次输入的密码都不正确!); System.exit(0); if(success) File file=new File(score1.txt); try FileReader inOne=new FileReader(file); BufferedReader inTow=new BufferedReader(inOne); String s=null; while(s=inTow.readLine()!=null) System.out.println(s); inOne.close();
14、inTow.close(); catch(IOException ioe) / score.txt:三, 抓举成绩 106kg, 挺举 189kg。:四, 抓举成绩 108kg, 挺举 186kg。:周五, 抓举成绩 112kg, 挺举 190kg。运行结果如图3所示图34实验后的练习:实验1 有如下格式的成绩单(文本格式)score.txt。:三,数学72分,物理67分,英语70分。:四,数学92分,物理98分,英语88分。:周五,数学68分,物理80分,英语77分。要求按行读入取成绩单,并在该行的后面尾加上该同学的总成绩,然后再将该行写入到一个名字为scoreAnalysis.txt的文件
15、中。/ AnalysisResult.javaimport java.io.*; import java.util.*;public class AnalysisResult public static void main(String args) File fRead=new File(score.txt); File fWrite=new File(scoreAnalysis.txt); try Writer out=new FileWriter(fWrite,true); BufferedWriter bufferWrite=new BufferedWriter(out); Reader
16、 in=new FileReader(fRead); BufferedReader bufferRead=new BufferedReader(in); String str=null; while(str=bufferRead.readLine()!=null) double totalScore=Fenxi.getTotalScore(str); str=str+总分:+totalScore; System.out.println(str); bufferWrite.write(str); bufferWrite.newLine(); bufferRead.close(); bufferW
17、rite.close(); catch(IOException e) System.out.println(e.toString(); / Fenxi.javaimport java.util.*;public class Fenxi public static double getTotalScore(String s) Scanner scanner=new Scanner(s); scanner.useDelimiter(0123456789.+); double totalScore=0; while(scanner.hasNext() try double score=scanner
18、.nextDouble(); totalScore=totalScore+score; catch(InputMismatchException exp) String t=scanner.next(); return totalScore; / score.txt:三,数学72分,物理67分,英语70分。:四,数学92分,物理98分,英语88分。:周五,数学68分,物理80分,英语77分。运行结果如图4所示图4实验2 按字典序输出全部不相同的单词。/ Dictionary.javaimport java.util.*;public class Dictionary public static
19、 void main(String args) Vector allWord,noSameWord; WordStatistic statistic=new WordStatistic(); statistic.setFileName(hello.txt); statistic.WordStatistic();/statistic调用WordStatistic()方法 allWord=statistic.getAllWord(); noSameWord=statistic.getNoSameWord(); System.out.println(共有+allWord.size()+个英文单词);
20、 System.out.println(有+noSameWord.size()+个互不相同英文单词); System.out.println(按字典顺序排列:); String s=new String noSameWord.size(); for(int i=0;inoSameWord.size();i+) si=noSameWord.elementAt(i); Arrays.sort(s); for(int i=0;inoSameWord.size();i+) System.out.println(si+ ); 运行结果如图5所示图5实验3 编写一个程序,程序运行时,要求用户输入的密码是:
21、hello。如果用户输入了正确的密码。程序将输出“你好,欢迎你!”。程序允许用户2次输入的密码不正确,一旦超过2次,程序将立刻退出。/ PassWord.javaimport java.io.*; import java.util.Scanner; public class PassWord public static void main(String args) int count=0; Console cons; String passwd; Scanner sc=new Scanner(System.in);/cons=System.console(); while(true) Syst
22、em.out.print(输入密码:); passwd=sc.next();/ count+; String password=new String (passwd); if(password.equals(hello) System.out.println(你好,欢迎你!); break; else System.out.println(您第+count+次输入的密+password+不正确); if(count=3) System.out.println(您+count+次输入的密码都不正确); System.exit(0); 运行结果如图6所示图65实验心得和体会这次实验中,我感觉比较难理解和掌握的就是Console类了,在实验3 密码流的代码在eclipse中运行出错如下图所示:就是输入不了密码,后来通过找寻解决方法,才知道了在JDK 6中java.io.Console类专用来访问基于字符的控制台设备,然后使用cmd命令提示符来运行就可以输入密码和显示结果了。通过这次实验,大概了解了字符输入、输出流用法,如何去使用Scanner类解析文件,对Console类的使用有个大概的认知。总而言之,这次实验收获蛮大的,敲代码的效率比以往提高了,出错率也少了。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1