1. 本周学习总结

2. 书面作业

将Student对象(属性:int id, String name,int age,double grade)写入文件student.data、从文件读出显示。
参考:本题具体要求见流与文件实验任务书-题目1-2.1

参考代码:TextFileTest.java

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

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

//201521123025
import java.io.*;
import java.util.*;

 class Student {
	private int id;
	private String name;
	private int age;
	private double grade;
	
	public Student(){
		
	}
	public Student(int id, String name, int age, double grade) {
		this.id = id;
		this.setName(name);
		this.setAge(age);
		this.setGrade(grade);
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		if (name.length()>10){
			throw new IllegalArgumentException("name's length should <=10 "+name.length());
		}
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		if (age<=0){
			throw new IllegalArgumentException("age should >0 "+age);
		}
		this.age = age;
	}
	public double getGrade() {
		return grade;
	}
	public void setGrade(double grade) {
		if (grade<0 || grade >100){
			throw new IllegalArgumentException("grade should be in [0,100] "+grade);
		}
		this.grade = grade;
	}
	
    @Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", grade=" + grade + "]";
	}
	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]);
	      
	   }
	
}

public class Test{
public static void main(String[] args){
	Student[] stu=new Student[3];
	stu[0]=new Student(1,"张三",19,65);
	stu[1]=new Student(2,"李四 ",19,75);
	stu[2]=new Student(25,"林振斌",20,85);
	try{
		PrintWriter out=new PrintWriter("d:/student.txt");
		Writedata(stu,out);
		out.close();
		Scanner in=new Scanner(new FileReader("d:/student.txt"));
		Student[] newstu=readData(in);
		in.close();
		for (Student e : newstu)
            System.out.println(e);
	}catch (IOException exception)
      {
         exception.printStackTrace();
      }
	
}

private static Student[] readData(Scanner in) {
	int n = in.nextInt();
      in.nextLine(); // consume newline

      Student[] stus = new Student[n];
      for (int i = 0; i < n; i++)
      {
         stus[i] = new Student();
         stus[i].readData(in);
      }
      return stus;
}

private static void Writedata(Student[] stu, PrintWriter out)throws IOException {
	 out.println(stu.length);

      for (Student e : stu)
         e.writeData(out);
}
}


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

id,age:每个数字占1byte
name:每个汉字占2byte
grade:每个grade占4字节
"|":每个占1byte
换行符:占2byte

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


0字节,因为数据写到缓冲区上,原来有close()方法自动调用flush()来刷新,但是现在没有close()了,所以数据在缓冲区上直接丢失了。

2. 缓冲流

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

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

用BufferedReader更快,因为使用缓冲区可以减少I/O次数,不用每次读写而是等缓冲区满了才进行一次读写。Scanner每次读写都要进行一次物理上的I/O操作,所以费时更多。

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

使用BufferedWriter后有提升,因为BufferedWriter有使用缓冲。

3. 字符编码

  • 参考:InputStreamReaderTest.java与教学PPT

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


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

  • 修改:

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

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

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

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




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

72个字节。一个int=4Byte,一个double=8Byte,一个汉字=2Byte,标识符有两个字节。一个学生24个字节,一共三名学生,72字节。
将该文件大小和题目1生成的文件对比是大了。因为使用UTF-8编码存储文件.

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

行数 分析
000行 31=>1,7C=>分隔符,D5 C5 C8 FD=>张三,31 39=>19,36 35 2E 30=>65.0,0D 0A=>换行
010行 32=>2, 7C=>分隔符,C0 EE CB C4 20=>李四,31 39=>19,37 35 2E 30=>75.0,0D 0A=>换行
020行 33=>3, 7C=>分隔符,CD F5 CE E5=>王五,32 30=>20,38 35 2E 30=>85.0,0D 0A=>换行

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

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.txt"));
out.wirteObject(student);
out.close();
objectInputStream in = new objectInputStream(new FileOutputStream("student.txt"));
Student[] new Student = (Student[])in.readOject();
in.close;
for(Student e :new Student)
	System.out.println(e);

Q5. Scanner基本概念组装对象

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

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

FileInputStream:从文件中读,BufferedInputStream:以缓冲方式从文件流中读,FileInputStream:因为要读UTF-8格式的文件

Q7. 文件操作

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

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

public static void findFile(String path,String filename){
    	 File pathName = new File(path);
    	    String[] fileName = pathName.list();
    	    for (String string : fileName) {
    	        File file1= new File(pathName.getAbsolutePath(),string);
    	        if (string.equals(filename)) {
    	            System.out.println(f.getAbsolutePath());
    	        }
    	        if (file1.isDirectory()) {
    	            findFile(file1.getAbsolutePath(),filename);
    	        }
    	    }

Q8. 正则表达式

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

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

public static boolean isnum(string str){
		String patternString="-?\\d+";
		return Pattern.matches(patternString, str);
	}

3. 码云及PTA