实验一:
目录
1.分析成绩单。现有如下格式的成绩单(文本格式)score.txt:
2. 统计英语单词。使用Scanner类和正则表达式统计一篇英文中的单词
3. 读取压缩文件。找一个压缩文件,如book.zip,并将book.zip中含有的文件重新存放到当前目录中的mybook文件夹中
1.题目要求:分析成绩单。现有如下格式的成绩单(文本格式)score.txt:
姓名:张三,数学72分,物理67分,英语70分。
姓名:李四,数学92分,物理98分,英语88分。
姓名:周五,数学68分,物理80分,英语77分。
要求按行读入成绩单,并在该行的后面结尾加上该同学的总成绩,然后再将该行写入到一个名字为scoreAnalysis.txt的文件中。
(1)实验运行结果截图:
(2)结果描述:
分析成绩单,运行结果得到一个名字为scoreAnalysis.txt的文件,是在原有的score.txt文件每行的后面尾加上该同学的总成绩保存得到的。
(3)源码:
AnalysisResult.java代码:
import java.io.*;
import java.util.StringTokenizer;
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); //以尾加方式创建指向文件fWrite的out流
BufferedWriter bufferWrite = new BufferedWriter(out); //创建指向out的bufferedWrite流
Reader in = new FileReader(fRead); //创建指向fRead的in流
BufferedReader bufferRead = new BufferedReader(in); //创建指向in的bufferRead流
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());
}
}
}
Fenxi.java代码:
mport java.util.InputMismatchException;
import java.util.Scanner;
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;
}
}
2. 题目要求 :统计英语单词。使用Scanner类和正则表达式统计一篇英文中的单词,要求如下:
a. 一共出现了多少个单词。
b.有多少个互不相同的单词。
c.按单词出现的频率大小输出单词。
(1)实验运行结果截图:
(2)结果描述:
统计英语单词,即统计文件中出现的单词总数以及互不相同的单词个数,按单词出现的频率大小排列输出单词。
(3)源码:
WordStatistic.java代码:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.Vector;
public class WordStatistic {
Vector<String> allWord,noSameWord;
File file = new File("english.txt");
Scanner sc = null;
String regex;
WordStatistic(){
allWord = new Vector<String>();
noSameWord = new Vector<String>();
//regex是由空格、数字和符号(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)组成的正则表达式
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());
}
}
public 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<String> getAllWord(){
return allWord;
}
public Vector<String> getNoSameWord(){
return noSameWord;
}
}
OutputWordMess.java代码:
import java.util.Vector;
public class OutputWordMess {
public static void main(String args[]){
Vector<String> 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()+"个英文单词");
System.out.println("有"+noSameWord.size()+"个互不相同英文单词");
System.out.println("按出现频率排列:");
int count[] = new int[noSameWord.size()];
for(int i = 0;i < noSameWord.size();i++){
String s1 = noSameWord.elementAt(i);
for(int j = 0;j < allWord.size();j++){
String s2 = allWord.elementAt(j);
if(s1.equals(s2))
count[i]++;
}
}
for(int m = 0;m < noSameWord.size();m++){
for(int n = m+1;n < noSameWord.size();n++){
if(count[n] > count[m]){
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 < noSameWord.size();m++){
double frequency = (1.0*count[m])/allWord.size();
System.out.printf("%s:%-7.3f",noSameWord.elementAt(m),frequency);
}
}
}
3. 题目要求:读取压缩文件。找一个压缩文件,如book.zip,并将book.zip中含有的文件重新存放到当前目录中的mybook文件夹中,即将book.zip的内容解压到mybook文件夹中。(book.zip为winzip压缩,包含目录没有测试!)
(1)实验运行结果截图:
(2)结果描述:
读取压缩文件,即把读取的压缩文件内容解压并重新存放到当前目录的自定义文件夹中。
(3)源码:
ReadZipFile.java代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ReadZipFile {
public static void main(String args[]){
File f = new File("D:\\大三下作业\\网络通信程序设计\\代码文件\\实验一\\book.zip");
File dir = new File("D:\\大三下作业\\网络通信程序设计\\代码文件\\实验一\\mybook");
byte b[] = new byte[100];
dir.mkdir();
try{
ZipInputStream in = new ZipInputStream(new FileInputStream(f));
ZipEntry zipEntry = null;
while ((zipEntry = in.getNextEntry()) != null){
File file = new File(dir,zipEntry.getName());
FileOutputStream out = new FileOutputStream(file);
int n = -1;
System.out.println(file.getAbsolutePath() + "的内容:");
while ((n = in.read(b,0,100)) != -1){
String str = new String(b,0,n);
System.out.println(str);
out.write(b,0,n);
}
out.close();
}
in.close();
}catch (IOException ee){
System.out.println(ee);
}
}
}