java-实验报告Word格式.doc
《java-实验报告Word格式.doc》由会员分享,可在线阅读,更多相关《java-实验报告Word格式.doc(16页珍藏版)》请在冰点文库上搜索。
FilefWrite=newFile("
scoreAnalysis.txt"
try{
Writerout=newFileWriter(fWrite,true);
//以尾加方式创建指向文件fWrite的out流
BufferedWriterbufferWrite=newBufferedWriter(out);
//创建指向out的bufferWrite流
Readerin=newFileReader(fRead);
//创建指向文件fRead的in流
BufferedReaderbufferRead=newBufferedReader(in);
//创建指向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(IOExceptione){
System.out.println(e.toString());
三、结果截图:
四、实验分析:
(1)改进程序,使得能统计出每个学生的平均成绩。
答:
(2)现在有如下格式的货物明细(文本格式)goods.txt
品名:
电视,length:
102cm,width:
89cm,height:
56cm.
轿车,length:
4502cm,width:
178cm,height:
156cm.
桌子,length:
125cm,width:
78cm,height:
68cm.
CalculateVolume.java
publicclassCalculateVolume{
goods.txt"
goodsVolume.txt"
try{Writerout=newFileWriter(fWrite,true);
while((str=bufferRead.readLine())!
doubles=Jisuan.getVolume(str);
体积:
+s+"
cm^3"
;
Jisuan.java
publicclassJisuan{
publicstaticdoublegetVolume(Strings){
doublevolume=1;
doubledistance=scanner.nextDouble();
volume=volume*distance;
returnvolume;
实验2统计英文单词
一、实验目的:
掌握使用Scanner类解析文件。
Dictionary.java:
publicclassDictionary{
Vector<
String>
allWord,noSameWord;
WordStatisticstatistic=newWordStatistic();
statistic.setFileName("
hello.txt"
statistic.wordStatistic();
//statistic调用WordStatistic()方法
allWord=statistic.getAllWord();
noSameWord=statistic.getNoSameWord();
System.out.println("
共有"
+allWord.size()+"
个英文单词"
有"
+noSameWord.size()+"
个互不相同英文单词"
按字典顺序排列:
Strings[]=newString[noSameWord.size()];
for(inti=0;
i<
noSameWord.size();
i++){
s[i]=noSameWord.elementAt(i);
Arrays.sort(s);
System.out.println(s[i]+"
"
OutputWordMess.java:
publicclassOutputWordMess{
按出现频率排列:
intcount[]=newint[noSameWord.size()];
Strings1=noSameWord.elementAt(i);
for(intj=0;
j<
allWord.size();
j++){
Strings2=allWord.elementAt(j);
if(s1.equals(s2))
count[i]++;
for(intm=0;
m<
m++){
for(intn=m+1;
n<
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;
}
doublefrequency=(1.0*count[m])/allWord.size();
System.out.printf("
%s:
%-7.3f"
noSameWord.elementAt(m),frequency);
WordStatistic.java:
publicclassWordStatistic{
Vector<
Filefile=newFile("
english.txt"
Scannersc=null;
Stringregex;
WordStatistic(){
allWord=newVector<
();
noSameWord=newVector<
//regex是由空格·
数字和符号(!
#$&
'
()*+,-./:
<
=>
?
@[\]^_`{|}~)组成的正则表达式
regex="
[\\s\\d\\p{Punct}]+"
sc=newScanner(file);
//创建指向file的sc
sc.useDelimiter(regex);
//sc调用useDelimiter(Stringregex)方法,向参数传递regex
catch(IOExceptionexp){
System.out.println(exp.toString());
voidsetFileName(Stringname){
file=newFile(name);
publicvoidwordStatistic(){
while(sc.hasNext()){
Stringword=sc.next();
allWord.add(word);
if(!
noSameWord.contains(word))
noSameWord.add(word);
catch(Exceptione){}
publicVector<
getAllWord(){
returnallWord;
getNoSameWord(){
returnnoSameWord;
四、实验分析:
按字典顺序输出全部不相同的单词。
Dictionary.java
实验3读取压缩文件
掌握ZipInputStream流的使用。
importjava.util.zip.*;
publicclassReadZipFile{
publicstaticvoidmain(Stringargs[]){
Filef=newFile("
book.zip"
Filedir=newFile("
mybook"
byteb[]=newbyte[100];
dir.mkdir();
try{
ZipInputStreamin=newZipInputStream(newFileInputStream(f));
ZipEntryzipEntry=null;
while((zipEntry=in.getNextEntry())!
=null){
Filefile=newFile(dir,zipEntry.getName());
FileOutputStreamout=newFileOutputStream(file);
intn=-1;
System.out.println(file.getAbsolutePath()+"
的内容:
while((n=in.read(b,0,100))!
=-1){
Stringstr=newString(b,0,n);
System.out.println(str);
out.write(b,0,n);
}
out.close();
}
in.close();
}
catch(IOExceptionee){
System.out.println(ee);
}
编写一个GUI程序,提供一个对话框,用户可以使用这个对话框选择要解压缩的Zip文件,设置解压后所得到的文件的存放目录。
答:
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassMyFrameextendsJFrameimplementsActionListener{
privatestaticfinallongserialVersionUID=1L;
privateJFileChooserfileChooser=newJFileChooser("
."
privateJButtonbutton=newJButton("
打开想要解压文件"
publicMyFrame(){
this.setTitle("
解压"
this.setPreferredSize(newDimension(500,400));
this.getContentPane().add(button,BorderLayout.CENTER);
button.addActionListener(this);
this.pack();
this.setLocationRelativeTo(null);
this.setIgnoreRepaint(true);
}
publicvoidactionPerformed(ActionEvente){
Objectsource=e.getSource();
if(sourceinstanceofJButton){
openFile();
}
publicvoidopenFile(){
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogTitle("
打开Zip文件夹"
intret=fileChooser.showOpenDialog(null);
if(ret==JFileChooser.APPROVE_OPTION){
System.out.println(fileChooser.getSelectedFile().getAbsolutePath());
Filef=newFile("
Filedir=newFile("
byteb[]=newbyte[100];
dir.mkdir();
try{ZipInputStreamin=newZipInputStream(newFileInputStream(f));
ZipEntryzipEntry=null;
while((zipEntry=in.getNextEntry())!
Filefile=newFile(dir,zipEntry.getName());
FileOutputStreamout=newFileOutputStream(file);
intn=-1;
System.out.println(file.getAbsolutePath()+"
的内容:
while((n=in.read(b,0,100))!
=-1){
Stringstr=newString(b,0,n);
System.out.println(str);
out.write(b,0,n);
}
out.close();
in.close();
catch(IOExceptionee){
System.out.println(ee);
publicstaticvoidmain(String[]args){
Frameframe=newMyFrame();
frame.setVisible(true);
补充1、用命令行带参的方式实现dir命令的功能,不带参数表示查询当前目录,带参数表示查询指定的目录,如dirc:
\,并支持dir*.java的文件后缀过滤功能。
Dir.java
importjava.text.*;
publicclassDir{
publicvoidGoBac