Java 文件输入输出流 实验题

实验一:

目录

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);
        }
    }
}
  • 16
    点赞
  • 104
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
3.2 习解析 1. 请设计一个Java应用程序,能够输入一个三位整数,然后输出该整数的各个数字,例如:输入123,则输出1、2、3。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入一个三位整数:"); int num = scanner.nextInt(); int digit1 = num / 100; int digit2 = num % 100 / 10; int digit3 = num % 10; System.out.println(digit1 + "、" + digit2 + "、" + digit3); } } ``` 2. 编写一个Java应用程序,能够输入学生的姓名和成绩,然后输出学生的姓名和成绩,最后输出所有学生的平均成绩。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入学生人数:"); int n = scanner.nextInt(); String[] names = new String[n]; double[] scores = new double[n]; double sum = 0; for (int i = 0; i < n; i++) { System.out.print("请输入第" + (i + 1) + "个学生的姓名:"); names[i] = scanner.next(); System.out.print("请输入第" + (i + 1) + "个学生的成绩:"); scores[i] = scanner.nextDouble(); sum += scores[i]; } double average = sum / n; System.out.println("所有学生的平均成绩为:" + average); for (int i = 0; i < n; i++) { System.out.println("第" + (i + 1) + "个学生的姓名为:" + names[i] + ",成绩为:" + scores[i]); } } } ``` 3. 编写一个Java应用程序,能够输入三个整数,然后输出它们的最大值和最小值。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入三个整数:"); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); int max = a; int min = a; if (b > max) { max = b; } if (c > max) { max = c; } if (b < min) { min = b; } if (c < min) { min = c; } System.out.println("最大值为:" + max); System.out.println("最小值为:" + min); } } ``` 4. 编写一个Java应用程序,能够输入一个字符串,然后输出字符串中所有的数字字符。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入一个字符串:"); String str = scanner.nextLine(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c >= '0' && c <= '9') { System.out.print(c + " "); } } } } ``` 5. 编写一个Java应用程序,能够输入一个字符串,然后输出字符串中所有的小写字母。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入一个字符串:"); String str = scanner.nextLine(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c >= 'a' && c <= 'z') { System.out.print(c + " "); } } } } ``` 6. 编写一个Java应用程序,能够输入一个字符串,然后输出字符串中所有的大写字母。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入一个字符串:"); String str = scanner.nextLine(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c >= 'A' && c <= 'Z') { System.out.print(c + " "); } } } } ``` 7. 编写一个Java应用程序,能够输入一个字符串,然后输出字符串中所有的字母字符。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入一个字符串:"); String str = scanner.nextLine(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { System.out.print(c + " "); } } } } ``` 8. 编写一个Java应用程序,能够输入一个字符串,然后输出字符串中所有的非字母字符。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入一个字符串:"); String str = scanner.nextLine(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) { System.out.print(c + " "); } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yscc-16

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值