201521123096《Java程序设计》第十二周学习总结

1. 本周学习总结

1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容。

2. 书面作业

将Student对象(属性:int id, String name,int age,double grade)写入文件student.data、从文件读出显示。

1. 字符流与文本文件:使用 PrintWriter(写),BufferedReader(读)

1.1 生成的三个学生对象,使用PrintWriter的println方法写入student.txt,每行一个学生,学生的每个属性之间用|作为分隔。使用Scanner或者BufferedReader将student.txt的数据读出。(截图关键代码,出现学号)

1.2 生成文件大小多少?分析该文件大小
68字节

1.3 如果调用PrintWriter的println方法,但在后面不close。文件大小是多少?为什么?

·参考:本题具体要求见流与文件实验任务书-题目1-2.1

·参考代码:TextFileTest.java
0字节,因为缓冲区的数据不会写进文件里。

2. 缓冲流

2.1 使用PrintWriter往文件里写入1千万行(随便什么内容都行),然后对比使用BufferedReader与使用Scanner从该文件中读取数据的速度(只读取,不输出),使用哪种方法快?请详细分析原因?提示:可以使用junit4对比运行时间

BufferedReader的读取速度快,因为它可以将数据先放在缓冲区,然后一次性读取。

2.2 将PrintWriter换成BufferedWriter,观察写入文件的速度是否有提升。记录两者的运行时间。试分析原因。

·参考:本题具体要求见流与文件实验任务书-题目1-2.2到2.3
·参考代码:BufferedReaderTest.java

写入文件速度有提升,因为BufferedWriter有缓冲区,减少了底层I/O操作。

3. 字符编码

3.1 现有EncodeTest.txt 文件,该文件使用UTF-8编码。使用FileReader与BufferedReader将EncodeTest.txt的文本读入并输出。是否有乱码?为什么会有乱码?如何解决?(截图关键代码,出现学号)
有乱码,因为FileReader按照GBK解码 ,但EncodeTest.txt文件是按照UTF-8进行编码。

3.2 编写一个方法convertGBK2UTF8(String src, String dst),可以将以GBK编码的源文件src转换成以UTF8编码的目的文件dst。

·参考:InputStreamReaderTest.java与教学PPT

4. 字节流、二进制文件:DataInputStream, DataOutputStream、ObjectInputStream

4.1 参考DataStream目录相关代码,尝试将三个学生对象的数据写入文件,然后从文件读出并显示。(截图关键代码,出现学号)

//201521123096
static final String dataFile = "Studentsdate";
    static final String[] name={"zhang","lin","zheng"};
    static final int[] age={21,23,12};
    static final double[] score={100,67,89};

    public static void main(String[] args) throws IOException {

 
        DataOutputStream out = null;
        
        try {
            out = new DataOutputStream(new
                    BufferedOutputStream(new FileOutputStream(dataFile)));

            for (int i = 0; i < age.length; i ++) {
                out.writeUTF(name[i]);
                out.writeInt(age[i]);
                out.writeDouble(score[i]);
            }
        } finally {
            out.close();
        }

        DataInputStream in = null;
        try {
            in = new DataInputStream(new
                    BufferedInputStream(new FileInputStream(dataFile)));
            String name;
            int age;
            double score;

            try {
                while (true) {
                    name = in.readUTF();
                    age = in.readInt();
                    score = in.readDouble();
                    System.out.println(name+" "+age+" "+score);
                }
            } catch (EOFException e) { }
        }
        finally {
            in.close();
        }
    }

4.2 生成的文件有多大?分析该文件大小?将该文件大小和题目1生成的文件对比是大了还是小了,为什么?
55字节,;变大了,因为题目1是用字符流写的,而该文件是用DataOutputStream 写入的,数字所占的字节不一样。

4.3 使用wxMEdit的16进制模式(或者其他文本编辑器的16进制模式)打开student.data,分析数据在文件中是如何存储的。

4.4 使用ObjectInputStream(读), ObjectOutputStream(写)读写学生。(截图关键代码,出现学号) //参考ObjectStreamTest目录

·参考:本题具体要求见流与文件实验任务书-题目1-1

//201521123096
static final String dataFile = "Studentsdate";
    static final String[] name={"zhang","lin","zheng"};
    static final int[] age={21,23,12};
    static final BigDecimal[] score={new BigDecimal("100.00"), 
            new BigDecimal("67.00"),
            new BigDecimal("89.00")};

    public static void main(String[] args) throws IOException , ClassNotFoundException{

 
        ObjectOutputStream out = null;
        
        try {
            out = new ObjectOutputStream(new
                    BufferedOutputStream(new FileOutputStream(dataFile)));

            for (int i = 0; i < age.length; i ++) {
                out.writeUTF(name[i]);
                out.writeInt(age[i]);
                out.writeObject(score[i]);
            }
        } finally {
            out.close();
        }

        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(new
                    BufferedInputStream(new FileInputStream(dataFile)));
            String name;
            int age;
            BigDecimal score;

            try {
                while (true) {
                    name = in.readUTF();
                    age = in.readInt();
                    score = (BigDecimal)in.readObject();
                    System.out.println(name+" "+age+" "+score);
                }
            } catch (EOFException e) { }
        }
        finally {
            in.close();
        }
    }

5. Scanner基本概念组装对象

编写public static List<Student> readStudents(String fileName)从fileName指定的文本文件中读取所有学生,并将其放入到一个List中。应该使用那些IO相关的类?说说你的选择理由。

·实验文件:Students.txt
·参考:TextFileTest目录下TextFileTest.java

7. 文件操作

编写一个程序,可以根据指定目录和文件名,搜索该目录及子目录下的所有文件,如果没有找到指定文件名,则显示无匹配,否则将所有找到的文件名与文件夹名显示出来。
7.1 编写public static void findFile(String path,String filename)函数,以path指定的路径为根目录,在其目录与子目录下查找所有和filename相同的文件名,一旦找到就马上输出到控制台。(截图关键代码,出现学号)

  1. 正则表达式

8.1 如何判断一个给定的字符串是否是10进制数字格式?尝试编程进行验证。(截图关键代码,出现学号)

  1. 码云及PTA

3.1. 码云代码提交记录

在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图

原文地址:https://www.cnblogs.com/sheyuvv/p/6850031.html