计通学院
《Java面向对象程序设计》实验
实验八输入输出流
先生姓名:××× 学号:××× 班级:××× 指点老师:××× 专业:计算机科学与技术
提交日期:×××年××月××日
实验陈述内容
1. 实验目的
把握字符输入、输出流用法; 把握使用Scanner类解析文件; 把握Console流的使用. 2. 实验内容
实验教材-第12章实验1、2、3
字符输入输出流;Scanner类和Console类.
请求:完美程序,给出实验结果截图;
完成试验后练习.
3. 程序代码及运转结果:
实验1 举重成绩单 import 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); BufferedWriter bufferWrite=new BufferedWriter(out); Reader 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(); bufferWrite.close(); } catch(IOException e){ System.out.println(e.toString()); } } }
import java.util.*; public class Fenxi {
public static double getTotalScore(String s) { String regex = \"[^0123456789.]\"; String digitMess = s.WordStrAll(regex,\"*\"); StringTokenizer fenxi = new StringTokenizer(digitMess,\"*\");
double totalScore=0;
while(fenxi.hasMoreTokens()){ double
score=Double.parseDouble(fenxi.nextToken()); totalScore=totalScore+score; }
return totalScore; } }
姓名:张三,抓举成绩 106kg,挺举 189kg. 姓名:李四,抓举成绩 108kg,挺举 186kg. 姓名:周五,抓举成绩 112kg,挺举 190kg. 运转结果如图1所示
图1
实验2 统计英文单词 import java.io.*; import java.util.*;
public class WordStatistic { Vector allWord=new Vector noSameWord=new Vector regex=\"[\\\\s\\\\d\\\\p{Punct}]+\";//正则表达式 try{ sc=new Scanner(file); sc.useDelimiter(regex); } catch(IOException exp){ System.out.println(exp.toString()); } } void setFileName(String name){ file=new File(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 Vector public Vector return noSameWord; } } import java.util.*; public class OutputWordMess { public static void main(String[] args) { Vector allWord=statistic.getAllWord(); noSameWord=statistic.getNoSameWord(); System.out.println(\"共有\"+allWord.size()+\"个英文单词\"); System.out.println(\"有\"+noSameWord.size()+\"个互不不异英文单词\"); System.out.println(\"按出现的频率排列:\"); int count[]=new int[noSameWord.size()]; for(int i=0;i String temp=noSameWord.elementAt(m); noSameWord.setElementAt(noSameWord.elementAt(n), m); noSameWord.setElementAt(temp, n); int t=count[m]; count[m]=count[n]; count[n]=t; } } } for(int m=0;m 运转结果如图2所示 图2 实验3 密码流 import java.io.*; public class PassWord { public static void main(String[] args) { boolean success=false; int count=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+\"次输入的密码\"+password+\"不准确!\"); }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(); inTow.close(); }catch(IOException ioe){} } } } 姓名:张三,抓举成绩 106kg,挺举 189kg. 姓名:李四,抓举成绩 108kg,挺举 186kg. 姓名:周五,抓举成绩 112kg,挺举 190kg. 运转结果如图3所示 图3 4. 实验后的练习: 实验1 有如下格式的成绩单(文本格式)score.txt. 姓名:张三,数学72分,物理67分,英语70分. 姓名:李四,数学92分,物理98分,英语88分. 姓名:周五,数学68分,物理80分,英语77分. 请求按行读入取成绩单,并在该行的后面尾加上该同学的总成绩,然后再将该行写入到一个名字为scoreAnalysis.txt的文件中. import 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 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(); bufferWrite.close(); } catch(IOException e){ System.out.println(e.toString()); } } } import 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.nextDouble(); totalScore=totalScore+score; } catch(InputMismatchException exp){ String t=scanner.next(); } } return totalScore; } } 姓名:张三,数学72分,物理67分,英语70分. 姓名:李四,数学92分,物理98分,英语88分. 姓名:周五,数学68分,物理80分,英语77分. 运转结果如图4所示 图4 实验2 按字典序输出全部不不异的单词. import java.util.*; public class Dictionary { public static void main(String args[]){ Vector 图5 实验3 编写一个程序,程序运转时,请求用户输入的密码是:hello.如果用户输入了准确的密码.程序将输出“你好,欢迎你!”.程序答应用户2次输入的密码不准确,一旦超出2次,程序将立刻退出. import 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){ System.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所示 图6 5. 实验心得和体会 此次实验中,我感觉比较难理解和把握的就是Console类了,在实验3 密码流的代码在eclipse中运转出错如下图所示: 就是输入不了密码,后来通过找寻解决方法,才晓得了在 JDK 6中java.io.Console类公用来访问基于字符的控制台设备,然后使用cmd命令提示符来运转就可以输入密码和显示结果了. 通过此次实验,大概了解了字符输入、输出流用法,如何去使用Scanner类解析文件,对Console类的使用有个大概的认知.总而言之,此次实验收获蛮大的,敲代码的效力比以往提高了,出错率也少了. 因篇幅问题不能全部显示,请点此查看更多更全内容