• 201521123100 《java程序设计》第12周学习总结


    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的数据读出。(截图关键代码,出现学号)
    A:

    //201521123100
          try
          {
             PrintWriter out = new PrintWriter("D:/student.date");
             out.println(student[0].id+"|"+student[0].name+"|"+student[0].age+"|"+student[0].grade);
             out.println(student[1].id+"|"+student[1].name+"|"+student[1].age+"|"+student[1].grade);
             out.println(student[2].id+"|"+student[2].name+"|"+student[2].age+"|"+student[2].grade);
             out.close();
             
             BufferedReader br=new BufferedReader(new FileReader("D:/student.date"));
             for(int i=0;i<3;i++){
             System.out.println(br.readLine());
             }
          }catch (IOException exception)
         {
             exception.printStackTrace();
          }
    

    运行结果

    1.2 生成文件大小多少?分析该文件大小
    A:68 字节
    1.3 如果调用PrintWriter的println方法,但在后面不close。文件大小是多少?为什么?

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

    参考代码:TextFileTest.java
    A:0,不使用close或者flush,缓冲区的数据是不会被写入文件中的

    2. 缓冲流

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

    原因:BufferedReader有缓冲区,不用按照实际IO次数读取,先放到缓冲区再一次性读取;而Scanner可能发生阻塞,等待信息进入,效率比较低
    2.2 将PrintWriter换成BufferedWriter,观察写入文件的速度是否有提升。记录两者的运行时间。试分析原因。
    参考:本题具体要求见流与文件实验任务书-题目1-2.2到2.3
    参考代码:BufferedReaderTest.java
    JUnit4常用注解
    JUnit4学习
    A:写入文件速度有提升

    原因:PrintWriter对整个文件进行缓冲;BufferedWriter将文本写入字符输出流,对各个字符进行缓冲

    3. 字符编码

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

    有乱码,因为FileReader并没有实现父类中带字符集参数的构造函数,只能按系统默认的字符集来解码

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

    参考:InputStreamReaderTest.java与教学PPT
    A:

    public static void convertGBK2UTF8(String src, String dst) throws IOException{
            try {
                BufferedReader  br=new BufferedReader(new FileReader(src));
                OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(dst));
                while(br.readLine()!=null){
                    osw.write(br.readLine());
                }
                br.close();
                osw.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch(IOException e1){
                e1.printStackTrace();
            }
    

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

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

    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生成的文件对比是大了还是小了,为什么?
    A:文件有55字节,和题目1生成的文件对比大了
    4.3 使用wxMEdit的16进制模式(或者其他文本编辑器的16进制模式)打开student.data,分析数据在文件中是如何存储的。
    A:数据是按照其类型所占字节大小,对应着一个十六进制值的方式来储存的
    4.4 使用ObjectInputStream(读), ObjectOutputStream(写)读写学生。(截图关键代码,出现学号) //参考ObjectStreamTest目录

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

    5. Scanner基本概念组装对象

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

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

    7. 文件操作

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

    7.2 加分点:使用队列、使用图形界面、使用Java NIO.2完成(任选1)

    参考代码:FindDirectories.java
    参考:本题具体要求见流与文件实验任务书-题目2
    A:

    8. 正则表达式

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

    3. 码云及PTA

    3.1. 码云代码提交记录

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

    3.2 PTA以前未完成的题目

    截图

  • 相关阅读:
    Atitit。D&D drag&drop拖拽功能c#.net java swing的对比与实现总结
    Atitit . 编程模型的变革总结
    Atitit.mysql oracle with as模式临时表模式 CTE 语句的使用,减少子查询的结构性 mssql sql server..
    Atitit.编程语言的主要的种类and趋势 逻辑式语言..函数式语言...命令式语言
    atitit.编程语言会形成进化树--哪些特性会繁荣??通才还是专才的选型 现代编程语言的特性总结
    Atitit.多媒体区----web视频格式的选择总结
    Atitit.web 视频播放器classid clsid 大总结quicktime,vlc 1. Classid的用处。用来指定播放器 1 2. <object> 标签用于包含对象,比如图像、音
    Atitit.部分错误 设计模式,异常处理框架atiPartErr 的总结
    Atitit.js javascript异常处理机制与java异常的转换 多重catc hDWR 环境 .js exception process Vob7
    atitit.判断时间重叠方法总结 java c++ c#.net js php
  • 原文地址:https://www.cnblogs.com/wangyan12345/p/6850134.html
Copyright © 2020-2023  润新知