欢迎来到天天文库
浏览记录
ID:56794233
大小:125.50 KB
页数:20页
时间:2020-07-12
《《Java面向对象程序设计》实验-实验八(输入输出流).doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、广西科技大学计通学院《Java面向对象程序设计》实验实验八输入输出流学生:×××学号:×××班级:×××指导老师:×××专业:计算机科学与技术提交日期:×××年××月××日实验报告容1.实验目的掌握字符输入、输出流用法;掌握使用Scanner类解析文件;掌握Console流的使用。2.实验容实验教材-第12章实验1、2、3字符输入输出流;Scanner类和Console类。要求:完善程序,给出实验结果截图;完成试验后练习。3.程序代码及运行结果:实验1举重成绩单//AnalysisResult.javaimportj
2、ava.io.*;importjava.util.*;publicclassAnalysisResult{publicstaticvoidmain(String[]args){FilefRead=newFile("score.txt");FilefWrite=newFile("scoreAnalysis.txt");try{Writerout=newFileWriter(fWrite);BufferedWriterbufferWrite=newBufferedWriter(out);Readerin=newFileR
3、eader(fRead);BufferedReaderbufferRead=newBufferedReader(in);Stringstr=null;while((str=bufferRead.readLine())!=null){doubletotalScore=Fenxi.getTotalScore(str);str=str+"总成绩:"+totalScore;System.out.println(str);bufferWrite.write(str);bufferWrite.newLine();}bufferR
4、ead.close();bufferWrite.close();}catch(IOExceptione){System.out.println(e.toString());}}}//Fenxi.javaimportjava.util.*;publicclassFenxi{publicstaticdoublegetTotalScore(Strings){Stringregex="[^0123456789.]";StringdigitMess=s.replaceAll(regex,"*");StringTokenizer
5、fenxi=newStringTokenizer(digitMess,"*");doubletotalScore=0;while(fenxi.hasMoreTokens()){doublescore=Double.parseDouble(fenxi.nextToken());totalScore=totalScore+score;}returntotalScore;}}//score.txt:三,抓举成绩106kg,挺举189kg。:四,抓举成绩108kg,挺举186kg。:周五,抓举成绩112kg,挺举190kg。
6、运行结果如图1所示图1实验2统计英文单词//WordStatistic.javaimportjava.io.*;importjava.util.*;publicclassWordStatistic{VectorallWord,noSameWord;Filefile=newFile("english.txt");Scannersc=null;Stringregex;WordStatistic(){allWord=newVector();noSameWord=newVector7、g>();regex="[\s\d\p{Punct}]+";//正则表达式try{sc=newScanner(file);sc.useDelimiter(regex);}catch(IOExceptionexp){System.out.println(exp.toString());}}voidsetFileName(Stringname){file=newFile(name);try{sc=newScanner(file);sc.useDelimiter(regex);}catch(IOExceptionex8、p){System.out.println(exp.toString());}}voidWordStatistic(){try{while(sc.hasNext()){Stringword=sc.next();allWord.add(word);if(!noSameWord.contains(word))noSameWord.add(word)
7、g>();regex="[\s\d\p{Punct}]+";//正则表达式try{sc=newScanner(file);sc.useDelimiter(regex);}catch(IOExceptionexp){System.out.println(exp.toString());}}voidsetFileName(Stringname){file=newFile(name);try{sc=newScanner(file);sc.useDelimiter(regex);}catch(IOExceptionex
8、p){System.out.println(exp.toString());}}voidWordStatistic(){try{while(sc.hasNext()){Stringword=sc.next();allWord.add(word);if(!noSameWord.contains(word))noSameWord.add(word)
此文档下载收益归作者所有