喵咪咪~

导航

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

答:

//201521123001
    Student[] students=new Student[3];
    students[0]=new Student(1,"张三",19,65);
    students[1]=new Student(2,"李四",19,75);
    students[2]=new Student(3,"王五",20,85);
    PrintWriter printWriter = new PrintWriter(new FileOutputStream("d:/student.txt")); 
    writeData(student, printWriter); 
    printWriter.close;
    Scanner in = new Scanner(new FileReader("d:/student.txt")); 
    Student[] newStudent = readData(in); 
    in.close();
    for (Student e : newStudent) 
        System.out.println(e);


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

答:

num 1字节4
name 4字节
3
age 2字节3
score 4字节
3
分隔符 3字节3
换行符 2字节
4
共51字节

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

参考:本题具体要求见流与文件实验任务书-题目1-2.1
参考代码:TextFileTest.java
答:0字节,因为PrintWriter的println方法将数据写到缓冲区中,还没有存入文件,只有调用flush方法的时候会强制将缓冲区中的数据写入文件,调用close时将自动调用flush,所以没有close时数据就丢失在缓冲区里了。

2. 缓冲流

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

答:用BufferedReader比用Scanner从该文件中读取数据的速度快很多,因为BufferedReader利用缓冲区减少I/O次数,可以在读取一定量内容以后再对文件进行操作。

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

参考:本题具体要求见流与文件实验任务书-题目1-2.2到2.3
参考代码:BufferedReaderTest.java
JUnit4常用注解
JUnit4学习
答:有提升,因为BufferedWriter利用了缓冲区减少I/O次数。

3. 字符编码

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

答:

有乱码,因为FileReader是按系统默认字符来解码的,可以用BufferedReader手动选择字符编码。

//201521123001
public static void main(String[] angs) throws IOException { 

	BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(new FileInputStream("EncodeTest.txt"), "UTF-8"));
	String line = null; 
	while ((line = bufferedReader.readLine() != null) { 
		System.out.println(line); 
	} 
	bufferedReader.close();
}

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

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

public static void convertGBK2UTF8(String src, String dst) throws IOException { 
    BufferedReader bufferedReader = new BufferedReader(new FileReader(src), "UTF-8");
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(dst), "UTF-8"); 
    String line = null; 
    while ((line = bufferedReader.readLine()) != null) { 
    outputStreamWriter.write(line);
    outputStreamWriter.newLine;
    }
bufferedReader.close(); 
outputStreamWriter.close();
}

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

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

答:

//201521123001

    DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("student.data"))); 
    writeData(students, dos); 
    dos.close(); 

    DatalnputStream dis= new DataInputStream(new BufferedInputStream(new FileInputStream("student.data"); 
    Student[] newStudent=readData(dis); 
    System.out.println(newStudent.length); 

    for (Student student : newStudent) { 
        System.out.printIn(student);
    }
    dis.close();

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

答:

变大了,因为用UTF-8编码存储文件,所占字节数变大了。

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

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

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

//201521123001
try{
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.txt")); 
    out.writeObject(student); 
    out.close();
    ObjectlnputStneam in = new ObjectInputStneam(new FileInputStneam("student.txt")); 
    Student[] newStudent = (Student[]) in.readObject(); 
    in.close(); 
    for (Student e : newStudent) 
        System.out.printIn(e);
}
catch(Exception e){
    e.printStackTrace();
}

5. Scanner基本概念组装对象

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

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

public static List<Student> readStudents (String fileName) throws IOException{ 
    ArrayList<Student>student = new ArrayList<Student>(); 
    try{ 
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader (new FilelnputStream("Student.txt"),"UTF-8")); 
        String string=null; 
        while((string=bufferedReader.readLine())!=null){ 
        String[] str = string.split(" "); 
        int num =Integer.parselnt (str[0]); 
        int age =Integer.parselnt(str[2]); 
        double score = Double.parseDouble(str[3]); 
        student.add (new Student (num , str[1] , age , score));
    }
    catch(IOException e){
        e.printStackTrace();
    }
    finally{
        if(bufferedReader!=null)
            bufferedReader.close();
    }
    return student;
}

7. 文件操作

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

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

答:

//201521123001
public static void findFile(String path,String fileName) { 
	File file1 = new File(path);
	String[] str = file1.list();
	
	for(String str : str) {
		File file2 = new File(file1.getAbsolutePath(), str);
		if(string.equals(fileName)) {
			system.out.println(file1.getAbsolutePath());
		}
		if(file2.isDirectory()) {
			findFile(file2.getAbsolutePath(), fileName);
		}
	}
}

8. 正则表达式

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

答:

public static boolean check (String string) { 
	for(int i = 0 ; i < string.length() ; i++) {
		char ch = string.charAt(i);
		if(ch%1!=0)
			return false;
	}
	return true;
}

3. 码云及PTA

3.1. 码云代码提交记录

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

posted on 2017-05-13 16:23  喵咪咪~  阅读(197)  评论(0编辑  收藏  举报