实验九 输入输出流.docx

上传人:b****6 文档编号:3341581 上传时间:2022-11-21 格式:DOCX 页数:15 大小:50.53KB
下载 相关 举报
实验九 输入输出流.docx_第1页
第1页 / 共15页
实验九 输入输出流.docx_第2页
第2页 / 共15页
实验九 输入输出流.docx_第3页
第3页 / 共15页
实验九 输入输出流.docx_第4页
第4页 / 共15页
实验九 输入输出流.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

实验九 输入输出流.docx

《实验九 输入输出流.docx》由会员分享,可在线阅读,更多相关《实验九 输入输出流.docx(15页珍藏版)》请在冰豆网上搜索。

实验九 输入输出流.docx

实验九输入输出流

实验九输入输出流

1.实验目的

1、掌握字符输入、输出流的用法

2、掌握使用Scanner类解析文件

3、掌握Console流的使用

2.实验内容

1、根据附录里的源代码,按照注释要求,完成代码填空,使程序能够运行得出结果。

1)实验1举重成绩单

2)实验2统计英文单词

3)实验3密码流

4)实验4学读汉字

5)实验5读取Zip文件

2、设计编写程序完成以下任务。

1)修改实验2,要求按字典顺序输出全部不相同的单词。

2)参考实验3编写一个程序,程序运行时,要求用户输入的密码是:

hello。

如果用户输入了正确的密码。

程序将输出“你好,欢迎你!

”。

程序允许用户2次输入的密码不正确,一旦超过2次,程序将立刻退出。

3)在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:

路径是xxx的文件夹内的文件有:

文件名:

abc.txt

路径名:

c:

\temp\abc.txt

--------------------------------------------

文件名:

def.txt

路径名:

c:

\temp\def.txt

知识点:

File管理

4)使用java的输入输出流将一个文本文件的内容读出并在控制台将文件中的内容打印出来。

(参考书上P289页)

5)从控制台输入一串字符串,将该串字符串写入到磁盘某位置上的out.txt文件中去。

(参考教材P290页)

6)使用java的输入输出流将一个文本文件的内容按行读取,每读一行就顺序添加行号,并写入到另一个文件中.

7)定义一个Person类,包含姓名、年龄、身份证号码属性。

有一个带参数的构造方法用于对所有属性赋值,还有一个study的方法,里面只打印一条“我喜欢学习”的话。

写一个Test类,里面实例化若干个Person对象,并将这些对象写入到obj.txt文件中去。

8)创建c:

/test.txt文件并在其中输入"helloworld"

创建一个输入流读取该文件中的文本

并且把小写的l变成大写L再利用输出流写入到d:

\test.txt中

实现步骤:

(1)在本地硬盘C盘下创建一个文件test.txt

(2)创建一个包含main()方法的类,并在main中编写代码

(3)运行代码并且测试结果

实现过滤器的功能

效果显示:

知识点:

IO字符流、String常用方法

3.实验步骤

4.评分标准

1.A——内容功能完善,编程风格好,人机接口界面好;

2.B——内容功能完善,编程风格良好,人机接口界面良好;

3.C——完成必做内容;

4.D——能完成必做内容;

5.E——未按时完成必做内容,或者抄袭(雷同者全部为E).

参照书上实验按模版要求,将【代码】替换为Java程序代码,编写好完整的程序文档,最后运行得到的相关文件,把实验所得文件一起打包上交。

(压缩包的文件名为:

学号后三位和名字开头字母,如109zhh.RAR|ZIP)

附录:

实验1举重成绩单

1.模板代码

AnalysisResult.java

importjava.io.*;

importjava.util.*;

publicclassAnalysisResult{

publicstaticvoidmain(String[]args){

try{

FilefRead=newFile("d:

\\score.txt");

FilefWrite=newFile("d:

\\scoreAnalysis.txt");

Writerout=//【代码1】//以尾加方式创建指向文件fWrite的out流

BufferedWriterbufferWrite=//【代码2】//创建指向out的bufferWrite流

Readerin=//【代码3】//创建指向文件fRead的in流

BufferedReaderbufferRead=//【代码4】//创建指向in的bufferRead流

Stringstr=null;

while((str=bufferRead.readLine())!

=null){

doubletotalScore=Fenxi.getTotalScore(str);

str=str+"总成绩:

"+totalScore;

System.out.println(str);

bufferWrite.write(str);

bufferWrite.newLine();

}

bufferRead.close();

bufferWrite.close();

}

catch(FileNotFoundExceptione){

System.out.println(e.toString());

}

catch(IOExceptione){

System.out.println(e.toString());

}

}

}

Fenxi.java

importjava.util.regex.*;

publicclassFenxi{

publicstaticdoublegetTotalScore(Strings){

Stringregex="\\d{1,5}";

doubletotalScore=0.0;

Patternp=Ppile(regex);

Matcherm=p.matcher(s);

while(m.find()){

try{

totalScore=totalScore+Double.parseDouble(m.group());

}

catch(NumberFormatExceptione){

System.out.println(e.getMessage());

}

}

returntotalScore;

}

}

实验2统计英文单词字

1.模板代码

WordStatistic.java

importjava.io.*;

importjava.util.Scanner;

importjava.util.Vector;

publicclassWordStatistic

{

VectorallWord,noSameWord;

Filefile=newFile("d:

\\english.txt");

Scannersc=null;

Stringregex;

WordStatistic()

{

allWord=newVector();

noSameWord=newVector();

regex="[\\s\\d\\p{Punct}]+";

try{

sc=//【代码1】//创建指向file的sc

//【代码2】//sc调用useDelimiter(Stringregex)方法,向参数传递regex

}catch(IOExceptionexp){

System.out.println(exp.toString());

}

}

voidsetFileName(Stringname){

file=newFile(name);

try{

sc=newScanner(file);

sc.useDelimiter(regex);

}catch(IOExceptionexp){

System.out.println(exp.toString());

}

}

publicvoidwordStatistic()

{

try{

while(sc.hasNext()){

Stringword=sc.next();

allWord.add(word);

if(!

noSameWord.contains(word)){

noSameWord.add(word);

}

}

}catch(Exceptione){}

}

publicVectorgetAllWord()

{returnallWord;

}

publicVectorgetNoSameWord()

{returnnoSameWord;

}

}

OutputWordMess.java

importjava.util.*;

publicclassOutputWordMess{

publicstaticvoidmain(Stringargs[]){

VectorallWord,noSameWord;

WordStatisticstatistic=newWordStatistic();

statistic.setFileName("d:

\\hello.txt");

//【代码3】//statistic调用wordStatistic方法

allWord=statistic.getAllWord();

noSameWord=statistic.getNoSameWord();

System.out.println("共有"+allWord.size()+"个英文单词");

System.out.println("有"+noSameWord.size()+"个互不相同英文单词");

System.out.println("按出现频率排列:

");

intcount[]=newint[noSameWord.size()];

for(inti=0;i

Strings1=noSameWord.elementAt(i);

for(intj=0;j

Strings2=allWord.elementAt(j);

if(s1.equals(s2)){

count[i]++;

}

}

}

for(intm=0;m

for(intn=m+1;n

if(count[n]>count[m]){

Stringtemp=noSameWord.elementAt(m);

noSameWord.setElementAt(noSameWord.elementAt(n),m);

noSameWord.setElementAt(temp,n);

intt=count[m];

count[m]=count[n];

count[n]=t;

}

}

}

for(intm=0;m

doublefrequency=(1.0*count[m])/allWord.size();

System.out.printf("%s:

%-7.3f",noSameWord.elementAt(m),frequency);

}

}

}

实验3密码流

1.模板代码

PassWord.java

importjava.io.*;

publicclassPassWord{

publicstaticvoidmain(String[]args){

booleansuccess=false;

intcount=0;

Consolecons;

char[]passwd;

cons=System.console();

while(true){

System.out.print("输入密码:

");

passwd=cons.readPassword();

count++;

Stringpassword=newString(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){

try{

Filefile=newFile("D:

\\score.txt");

FileReaderinOne=newFileReader(file);

BufferedReaderinTow=newBufferedReader(inOne);

Strings=null;

while((s=inTow.readLine())!

=null){

System.out.println(s);

}

inOne.close();

inTow.close();

}catch(FileNotFoundExceptionfe){

System.out.println(fe.toString());

}

catch(IOExceptionioe){

System.out.println(ioe.toString());

}

}

}

}

 

实验4学读汉字

1.模板代码

ChineseCharacters.java

importjava.io.*;

importjava.util.StringTokenizer;

publicclassChineseCharacters

{publicStringBuffergetChinesecharacters(Filefile)

{StringBufferhanzi=newStringBuffer();

try{FileReaderinOne=【代码1】//创建指向文件f的inOne的对象

BufferedReaderinTwo=【代码2】//创建指向文件inOne的inTwo的对象

Strings=null;

inti=0;

while((s=【代码3】)!

=null)//inTwo读取一行

{StringTokenizertokenizer=newStringTokenizer(s,",'\n'");

while(tokenizer.hasMoreTokens())

{hanzi.append(tokenizer.nextToken());

}

}

}

catch(Exceptione){}

returnhanzi;

}

}

StudyFrame.java

importjava.awt.*;

importjava.awt.event.*;

importjava.io.*;

importjavax.sound.sampled.*;

publicclassStudyFrameextendsFrameimplementsItemListener,ActionListener,Runnable

{ChineseCharacterschinese;

Choicechoice;

ButtongetCharacters,voiceCharacters;

LabelshowCharacters;

StringBuffertrainedChinese=null;

Clipclip=null;

ThreadvoiceThread;

intk=0;

PanelpCenter;

CardLayoutmycard;

TextAreatextHelp;

MenuBarmenubar;

Menumenu;

MenuItemhelp;

publicStudyFrame()

{chinese=newChineseCharacters();

choice=newChoice();

choice.add("training1.txt");

choice.add("training2.txt");

choice.add("training3.txt");

showCharacters=newLabel("",Label.CENTER);

showCharacters.setFont(newFont("宋体",Font.BOLD,72));

showCharacters.setBackground(Color.green);

getCharacters=newButton("下一个汉字");

voiceCharacters=newButton("发音");

voiceThread=newThread(this);

choice.addItemListener(this);

voiceCharacters.addActionListener(this);

getCharacters.addActionListener(this);

PanelpNorth=newPanel();

pNorth.add(newLabel("选择一个汉字字符组成的文件"));

pNorth.add(choice);

add(pNorth,BorderLayout.NORTH);

PanelpSouth=newPanel();

pSouth.add(getCharacters);

pSouth.add(voiceCharacters);

add(pSouth,BorderLayout.SOUTH);

pCenter=newPanel();

mycard=newCardLayout();

pCenter.setLayout(mycard);

textHelp=newTextArea();

pCenter.add("hanzi",showCharacters);

pCenter.add("help",textHelp);

add(pCenter,BorderLayout.CENTER);

menubar=newMenuBar();

menu=newMenu("帮助");

help=newMenuItem("关于学汉字");

help.addActionListener(this);

menu.add(help);

menubar.add(menu);

setMenuBar(menubar);

setSize(350,220);

setVisible(true);

addWindowListener(newWindowAdapter()

{publicvoidwindowClosing(WindowEvente)

{System.exit(0);

}

});

validate();

}

publicvoiditemStateChanged(ItemEvente)

{StringfileName=choice.getSelectedItem();

Filefile=newFile(fileName);

trainedChinese=chinese.getChinesecharacters(file);

k=0;

mycard.show(pCenter,"hanzi");

}

publicvoidactionPerformed(ActionEvente)

{if(e.getSource()==getCharacters)

{if(trainedChinese!

=null)

{charc=trainedChinese.charAt(k);

k++;

if(k>=trainedChinese.length())

k=0;

showCharacters.setText(""+c);

}

else

{showCharacters.setText("请选择一个汉字字符文件");

}

}

if(e.getSource()==voiceCharacters)

{if(!

(voiceThread.isAlive()))

{voiceThread=newThread(this);

}

try{voiceThread.start();

}

catch(Exceptionexp){}

}

if(e.getSource()==help)

{mycard.show(pCenter,"help");

try{FilehelpFile=newFile("help.txt");

FileReaderinOne=【代码4】//创建指向文件helpFile的inOne的对象

BufferedReaderinTwo=【代码5】//创建指向文件inOne的inTwo的对象

Strings=null;

while((s=inTwo.readLine())!

=null)

{textHelp.append(s+"\n");

}

inOne.close();

inTwo.close();

}

catch(IOExceptionexp){}

}

}

publicvoidrun()

{voiceCharacters.setEnabled(false);

try{if(clip!

=null)

{clip.close()

}

clip=AudioSystem.getClip();

FilevoiceFile=newFile(showCharacters.getText().trim()+".wav");

clip.open(AudioSystem.getAudioInputStream(voiceFile));

}

catch(Exceptionexp){}

clip.start();

voiceCharacters.set

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1