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

//201521123117
Student类:
         public void writeData(PrintWriter out)
       {
          out.println(id + "|" + name + "|" + age + "|" + grade );
       }

     public void readData(Scanner in)
     {
          String line = in.nextLine();
          String[] tokens = line.split("\\|");
          id =Integer.parseInt(tokens[0]);
          name =tokens[1];
          age = Integer.parseInt(tokens[2]);
          grade = Double.parseDouble(tokens[3]);
       }

Main函数:
 try
          {
             // save all employee records to the file employee.txt
             PrintWriter out = new PrintWriter("student.txt");
             writeData(stu, out);
             out.close();

             // retrieve all records into a new array
             Scanner in = new Scanner(new FileReader("student.txt"));
             Student[] newStaff = readData(in);
             in.close();

             for (Student e : newStaff)
                System.out.println(e);
          }
          catch (IOException exception)
          {
             exception.printStackTrace();
          }
       }
    private static void writeData(Student[] students, PrintWriter out) throws IOException
       {
          out.println(students.length);
          for (Student e : students)
             e.writeData(out);
       }

       
       private static Student[] readData(Scanner in)
       {
          int n = in.nextInt();
          in.nextLine(); 
          Student[] students = new Student[n];
          for (int i = 0; i < n; i++)
          {
              students[i] = new Student();
              students[i].readData(in);
          }
          return students;
       }

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

生成文件大小:50字节
分析:第一行的“3”代表数组的大小,占3个字节;一个id占1个字节,共占3个字节;英文字符串每个字母占1个字节,故共占11个字节;age占2个字节,共6个字节;grade占4个字节,共占12个字节;分隔符有3个,共占3个字节;行末尾共占2个字节。

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

0字节
close方法中会自动调用flush()函数,flush()函数作用为清空缓冲区数据。若调用PrintWriter的println方法,但在后面不close的话,缓冲区中的数据就会丢失。

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

参考代码:TextFileTest.java

2. 缓冲流

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

BufferedReader是在字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取,使用缓冲可以减少IO次数。读取数据的速度要比Scanner块。

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

使用BufferedWriter写入文件的速度要更快;因为BufferedWriter使用了缓冲技术。

参考:本题具体要求见流与文件实验任务书-题目1-2.2到2.3
参考代码:BufferedReaderTest.java
JUnit4常用注解
JUnit4学习

3. 字符编码

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

出现乱码;因为FileReader会按照系统默认的字符集(如GBK)来解码,但是EncodeTest.txt 文件使用了UTF-8编码。将UTF-8编码的字符使用GBK编码来解析,会出现乱码情况。

解决办法

//201521123117	
pubilc class InputStreamReaderTest{
    public static void main(Srting[] args) throws Exception{
          BuffererdReader br = null;
          try {
               br = new BufferedReader(new InputStreamReader(new FileInputStream("d:/EncodeTest.txt"),"UTF-8));
               String line = null;
               while((line=br.readLine())!=null)
                   System.out.println(line);
           }finally{
                if (br!=null){
                    br.close();
                }
           }
      }
}

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

//201521123117	
public static void main(Srting[] args) throws Exception{
          String dst = "e:/dst.txt";
          String src = "e:/src.txt";
          convertGBK2UTF8( src, dst);
}
public static void convertGBK2UTF8(String src, String dst)throws IOException{
          BufferedReader br = null;
          br = new BufferedReader(new FileReader(src));
          OutputStreamWriter osw=new OutputStreamWriter(FlieOutputStream(dst),"UTF-8");
          String line =null;
          while((line=br.readLine())!=null){
            osw.write(line+"\n");
          }
         br.close();
        osw.close();      

参考:InputStreamReaderTest.java与教学PPT

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

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

Student[] student=new Student[3];
		student[0] = new Student(1,"zheng", 20,98);
		student[1] = new Student(2,"chen",19,80);
		student[2] = new Student(3,"li",21,85);
 //201521123117
           DataOutputStream dos=null;
           try{
        	   dos=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("student2.txt")));
               for(int i=0;i<3;i++){
            	   dos.writeInt(student[i].getId());
            	   dos.writeUTF(student[i].getName());
            	   dos.writeInt(student[i].getAge());
            	   dos.writeDouble(student[i].getGrade());
               }
           }finally{
        	   dos.close();
           }
//201521123117
           DataInputStream dis=null;
           try{
        	   dis=new DataInputStream(new BufferedInputStream(new FileInputStream("student2.txt")));
        	   Student[] student2=new Student[3];
               for(int i=0;i<3;i++){
            	   Student student1=new Student(dis.readInt(),dis.readUTF(),dis.readInt(),dis.readDouble());
            	   student2[i]=student1;
               }
               for(int i=0;i<3;i++)
               {
            	   System.out.println("student"+"[id=" + student2[i].getId() + ",name=" + student2[i].getName() + ",age=" +  
                       student2[i].getAge()+",grade="+ student2[i].getGrade()+"]");
               }
           }catch(Exception e){
        	   System.out.println(e);
           }finally{
        	   dis.close();
           }

4.2 生成的文件有多大?分析该文件大小?将该文件大小和题目1生成的文件对比是大了还是小了,为什么?

生成文件大小:65字节
分析:一个int型4字节,id和age共24字节;一个double型8字节,grade共24字节;一个英文字符1字节,name共11字节;一个标识符2字节,共6字节;故文件大小为65字节。
将该文件大小和题目1生成的文件对比是大了。因为DataOutputStream写入文件是按照不同的数据类型写入,每个数据类型占一定的大小,如int占4个字节。

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


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

//201521123117
    ObjectOutputStream out =new ObjectOutputStream(new FileOutputStream("student.txt"));
    out.writeObject(student);
    out.close();
    
    ObjectInputStream in =new ObjectInputStream(new FileInputStream("student.txt"));
    Student[] newStudent =(Student[]) in.readObject();
    in.close();
    for(Student e: newStudent)
      System.out.println(e);

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

5. Scanner基本概念组装对象

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

运行结果:

FileInputStream:从文件中读取数据;
InputStreamReader:读UTF-8格式的文件;
BufferedReader:使用缓冲技术,读取速度更快;

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

7. 文件操作

编写一个程序,可以根据指定目录和文件名,搜索该目录及子目录下的所有文件,如果没有找到指定文件名,则显示无匹配,否则将所有找到的文件名与文件夹名显示出来。

7.1 编写public static void findFile(String path,String filename)函数,以path指定的路径为根目录,在其目录与子目录下查找所有和filename相同的文件名,一旦找到就马上输出到控制台。(截图关键代码,出现学号)

//201521123117
public class findFilename{
      public static void main(String[] args){
          //TODO Auto-generated method stub
        findFile("E:\\作业","src.txt");
       }

       public static void findFile(String path,String filename) {
           //TODO Auto-generated method stub
           File file =new File(path);
           String[] fileName = file.list();
           for(String string:fileName){
               File files =new File(file.getAbsolutePath(),string);
               if(string.equals(filename))
                   System.out.println(files.getAbsolutePath());
               if(files.isDirectory())
                   findFile(files.getAbsolutePath(),filename);
           }
       }

}

运行结果:

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

关键代码:

//201521123117
    Queue<File> queue=new LinkedList<>();
    queue.offer(new File(path));
    while(!queue.isEmpty()){
        File file =queue.poll();
        String[] fileName = file.list();
        for(String string:fileName){
            File files =new File(file.getAbsolutePath(),string);
            if(string.equals(filename))
                System.out.println(files.getAbsolutePath());
            if(files.isDirectory())
                queue.offer(files);
    }
 }

运行结果:

参考资料:判断文件的编码格式

8. 正则表达式

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

//201521123117
Public class checknumber{
   Public static void main (string[] aegs) {
      //TODO Atuto-generated method stub
   Scanner in =new Scanner(System.in);
   While(in.hasNext()){
   String str=in.nextline();
   System .out.printLn(checknumber(str));
  }
 }
   public static boolean checknumber (string str){
    String patternstring ="[+-]?[0-9]";
    return pattern.matches(patternstring,str);
 }
}

运行结果:

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

3. 码云及PTA

posted on 2017-05-13 20:44  呼吸一下广阔的天气  阅读(201)  评论(0编辑  收藏  举报