Java第09次实验(IO流)--实验报告

0.字节流与二进制文件

我的代码

  • 用DataOutputStream和FileOutputStream将Student对象写入二进制文件student.data
package test;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class StudentFile {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String fileName = "d:/student.data";
        try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(fileName))) {
            Student stu1 = new Student(1, "wang", 19, 89);
            dos.writeInt(stu1.getId());
            dos.writeUTF(stu1.getName());
            dos.writeInt(stu1.getAge());
            dos.writeDouble(stu1.getGrade());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try (DataInputStream dis = new DataInputStream(new FileInputStream(fileName))) {
            int id = dis.readInt();
            String name = dis.readUTF();
            int age = dis.readInt();
            double grade = dis.readDouble();
            Student stu = new Student(id, name, age, grade);
            System.out.println(stu);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我的总结

  • 二进制

1.字符流与文本文件

我的代码

  • 使用BufferedReader从编码为UTF-8的文本文件中读出学生信息,并组装成对象然后输出。
package test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String fileName="d:/Students.txt";
        List<Student> studentList = new ArrayList<>();
                try(
                    FileInputStream fis=new FileInputStream(fileName);
                    InputStreamReader isr=new InputStreamReader(fis, "UTF-8");
                    BufferedReader br=new BufferedReader(isr))
                {
                    String line=null;
                    while((line=br.readLine())!=null)
                    {
                        String[] msg=line.split("\\s+");
                        int id=Integer.parseInt(msg[0]);
                        String name=msg[1];
                        int age=Integer.parseInt(msg[2]);
                        double grade=Double.parseDouble(msg[3]);
                        Student stu=new Student(id,name,age,grade);
                        studentList.add(stu);
                    }
                } 
                catch (FileNotFoundException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
                catch (IOException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(studentList);

    }
}
  • 编写public static ListreadStudents(String fileName);从fileName指定的文本文件中读取所有学生,并将其放入到一个List中
public static List<Student> readStudents(String fileName)
    {
        List<Student> stuList = new ArrayList<>();
        try(
            FileInputStream fis=new FileInputStream(fileName);
            InputStreamReader isr=new InputStreamReader(fis, "UTF-8");
            BufferedReader br=new BufferedReader(isr))
        {
            String line=null;
            while((line=br.readLine())!=null)
            {
                String[] msg=line.split("\\s+");
                int id=Integer.parseInt(msg[0]);
                String name=msg[1];
                int age=Integer.parseInt(msg[2]);
                double grade=Double.parseDouble(msg[3]);
                Student stu=new Student(id,name,age,grade);
                stuList.add(stu);
            }
        } 
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return stuList;
    }
  • 使用PrintWriter将Student对象写入文本文件
package test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class WriteFile {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String fileName = "d:/Students.txt";
        try (FileOutputStream fos = new FileOutputStream(fileName, true);
                OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
                PrintWriter pw = new PrintWriter(osw)) {
            pw.println("1 zhang 18 85");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
  • 使用ObjectInputStream/ObjectOutputStream读写学生对象。
package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class WriteFile {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String fileName="d:/Students.dat";
        try(
                    FileOutputStream fos=new FileOutputStream(fileName);
                    ObjectOutputStream oos=new ObjectOutputStream(fos))
                {
                    Student ts=new Student(1,"lily",64,90);
                    oos.writeObject(ts);
                }
                catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try(
                    FileInputStream fis=new FileInputStream(fileName);
                    ObjectInputStream ois=new ObjectInputStream(fis))
                {
                    Student newStudent =(Student)ois.readObject();
                    System.out.println(newStudent);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    }

}

我的总结

2.缓冲流

我的代码

  • 使用PrintWriter往文件里写入一千万行的随机整数,范围在[0,10],随机种子为100.
package test;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Random;

public class WriteFile {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String fileName = "d:/bigdata.txt";
        int n = 1000_0000;
        Random r = new Random(100);
        try(PrintWriter pw = new PrintWriter(fileName)){
            for(int i=0;i<n;i++) {
                pw.println(r.nextInt(11));
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
  • JUNIT测试部分,测试BufferedReader与Scanner的读文件的效率
package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

import org.junit.jupiter.api.Test;

class TestRead {
    String fileName = "d:/bigdata.txt";
    
    /*BufferedReader读取文件*/
    @Test
    void testB() {
        try(BufferedReader br = new BufferedReader(new FileReader(new File(fileName)))){
            String str = null;
            int count = 0;
            long sum = 0;
            while((str = br.readLine()) != null) {
                int num = Integer.parseInt(str);
                sum += num;
                count++;
            }
            System.out.format("count=%d,sum=%d,average=%.5f\n",count,sum,sum*1.0/count);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    /*Scanner读取文件*/
    @Test
    void testS() {
        try(Scanner sc = new Scanner(new File(fileName))){
            int count = 0;
            long sum = 0;
            while(sc.hasNextLine()) {
                String str = sc.nextLine();
                int num = Integer.parseInt(str);
                count++;
                sum += num;
            }
            System.out.format("count=%d,sum=%d,average=%.5f\n",count,sum,sum*1.0/count);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
  • JUNIT测试结果

我的总结

3.字节流之对象流

结合使用ObjectOutputStream、ObjectInputStream与FileInputStream、FileOuputStream实现对Student对象的读写。
编写如下两个方法:

  • public static void writeStudent(List stuList)
  • public static List readStudents(String fileName)

    我的代码

    public static void writeStudent(List<Student> stuList)
    {
        String fileName="d:/Students.dat";
        try (   FileOutputStream fos=new FileOutputStream(fileName);
                ObjectOutputStream ois=new ObjectOutputStream(fos))
        {
            ois.writeObject(stuList);
            
        } 
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
public static List<Student> readStudents(String fileName)
    {
        List<Student> stuList=new ArrayList<>();
        try (   FileInputStream fis=new FileInputStream(fileName);
                ObjectInputStream ois=new ObjectInputStream(fis))
        {
            stuList=(List<Student>)ois.readObject();
        } 
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return stuList;
    } 

我的总结

5.文件操作

编写一个程序,可以根据指定目录和文件名,搜索该目录及子目录下的所有文件,如果没有找到指定文件名,则显示无匹配,否则将所有找到的文件名与文件夹名显示出来。
编写public static void findFile(Path dir,String fileName)方法.
以dir指定的路径为根目录,在其目录与子目录下查找所有和filename
相同的文件名,一旦找到就马上输出到控制台。

我的代码

递归方法

package test;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Path dir = Paths.get("D:\\", "testStream", "5");
        findFile(dir, "c.txt");
    }

    public static void findFile(Path dir, String fileName) {
        File file = dir.toFile();
        File[] files = file.listFiles();
        for (File now : files) {
            if (now.isFile()) {
                if (now.getName().equals(fileName)) {
                    System.out.println(now.getAbsolutePath());
                    return;
                }
            } else if (now.isDirectory()) {
                findFile(now.toPath(), fileName);
            }
        }
    }
}

我的总结

6.正则表达式

我的代码

  • 如何判断一个给定的字符串是否是10进制数字格式?尝试编程进行验证。
package test;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Pattern pattern=Pattern.compile("^[+-]?[0-9]+(\\.\\d+)?");
        Matcher matcher=null;
        while(sc.hasNext())
        {
            String str=sc.next();
            matcher=pattern.matcher(str);
            System.out.println(matcher.matches());
        }
        sc.close();
    }

}
  • 修改HrefMatch.java
    • 匹配网页中的数字字符串
package test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class HrefMatch {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try
          {
             // get URL string from command line or use default
             String fileName="D:\\test\\File\\HrefMatch.htm";
             // open reader for URL
             InputStreamReader in = new InputStreamReader(new FileInputStream(fileName));
             // read contents into string builder
             StringBuilder input = new StringBuilder();
             int ch;
             while ((ch = in.read()) != -1)
                input.append((char) ch);

             String patternString = "[+-]?[0-9]+(\\.\\d+)?";
             Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
             Matcher matcher = pattern.matcher(input);

             while (matcher.find())
             {
                int start = matcher.start();
                int end = matcher.end();
                String match = input.substring(start, end);
                System.out.println(match);
             }
          }
          catch (IOException e)
          {
             e.printStackTrace();
          }
          catch (PatternSyntaxException e)
          {
             e.printStackTrace();
          }
    }
}

我的总结

猜你喜欢

转载自www.cnblogs.com/Lay-549/p/11938841.html