【Java】第10章_Stream 上机实践与习题

实验1 分析成绩单

new FileWriter(fwrite, true);	//代码1	以尾加方式创建指向文件fWrite的out流

new BufferedWriter(out);	//代码2	创建指向out的bufferWrite流

new FileReader(fRead);	//代码3	创建指向文件fRead的in流

new BufferedReader(in);	//代码4	创建指向in的bufferRead流
  • 实验后练习
    总分除以3;

实验2 统计英文单词

new Scanner(file);	//代码1	创建指向file的sc

sc.useDelimiter(regex);	//代码2	sc调用useDelimiter(String regex)方法,向参数传递regex

statistic.wordStatistic();	//代码3	statistic调用wordStatistic()方法

习题10

  • 1.应当使用FileInputStream流
  • 2.FileInputStream流的read方法,以字节为单位;
    FileReader流的read方法,以字符为单位;
  • 3.只能指向字符流;
  • 4.注意对象序列化;
  • 5.先用对象输出流将对象写入目的地,再以目的地为源用输入流读出对象的序列化信息;
    根据对象的序列化信息创建一个对象;
  • 6.代码:

BackwardRead.java

package randomAccessFile;

import java.io.IOException;
import java.io.RandomAccessFile;

public class BackwardRead {
    public static void main (String args[]) {
        RandomAccessFile inAndOut = null;
        int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        try {
            inAndOut = new RandomAccessFile("tom.dat", "rw");
            for(int i = 0; i < data.length; i++) {
                inAndOut.writeInt(data[i]);
            }
            for(long i = data.length - 1; i >= 0; i--) {
                inAndOut.seek(i * 4);
                System.out.printf("\t%d", inAndOut.readInt());
            }
            inAndOut.close();
        }
        catch (IOException e) {};
    }
}

  • 7.BufferedReader流能够读取文本行,方法readLine()
    换行符,newLine();以字符串为单位写入write()
  • 8.过滤输出流:

PrintStreamTest.java

package printStream;

import java.io.*;

public class PrintStreamTest {
    public static void main(String args[]) {
        try {
            File file = new File("p.txt");
            FileOutputStream out = new FileOutputStream(file);
            PrintStream ps = new PrintStream(out);
            ps.print(123645.6789);
            ps.println("how are you");
            ps.println(true);
            ps.close();
        }
        catch (IOException e) {}
    }
}

ScannerParseFile.java

package parsingFile;

import java.io.*;
import java.util.Scanner;

public class ScannerParseFile {
    public static void main (String args[]) {
        File file = new File("parseFile.txt");
        String content[] = {"商品列表:", "电视机,2567元/台", "洗衣机,3562元/台", "冰箱,6573元/台"};
        try {
            FileWriter fw = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(fw);
            for(String s : content) {
                bw.write(s);
                bw.newLine();
            }
            bw.close();
            fw.close();
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String s,
                    regex = "[^0123456789.]+";
            double sum = 0;
            while((s = br.readLine()) != null) {
                Scanner scanner = new Scanner(s);
                scanner.useDelimiter(regex);
                while (scanner.hasNext()) {
                    double price = scanner.nextDouble();
                    System.out.println(price);
                    sum = sum + price;
                }
            }
            System.out.println("平均价格:" + sum / 3);
            br.close();
            fr.close();
        }catch (IOException e) {
            System.out.println(e);
        }
    }
}
//用Scanner解析文件,文件中一旦有汉字就不行了
//        String regex = "[^0123456789]+";
//        File file = new File("parseFile.txt");
//        Scanner scanner = null;
//        double sum = 0;
//        try {
//            scanner = new Scanner(file);
//            scanner.useDelimiter(regex);
//            while(scanner.hasNext()) {
//                try {
//                    double price = scanner.nextDouble();
//                    sum = sum + price;
//                    System.out.println(price);
//                }
//                catch (InputMismatchException ime) {
//                    String t = scanner.next();
//                }
//            }
//            System.out.println("平均价格:" + sum / 3);
//        }
//        catch (Exception e) {
//            System.out.println(e);
//        }
发布了77 篇原创文章 · 获赞 160 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43763494/article/details/104232065
今日推荐