Java十二周总结

时间:2024-01-01 14:55:57

1. 本周学习总结

1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容。

Java十二周总结

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

Java十二周总结

Java十二周总结

	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]);
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", grade=" + grade + "]";
} }
import java.io.*;
import java.util.*; /**
* @version 1.12 2007-06-22
* @author Cay Horstmann
*/
public class TextFileTest
{
public static void main(String[] args)
{
Student[] staff = new Student[3];
staff[0] = new Student(43,"Yang", 20,2015);
staff[1] = new Student(2,"Harry", 16,3);
staff[2] = new Student(3,"Tony", 18,3); /**
* Writes all employees in an array to a print writer
* @param staff an array of employees
* @param out a print writer
*/
private static void writeData(Student[] staff, PrintWriter out) throws IOException
{
// write number of employees
out.println(staff.length); for (Student e : staff)
e.writeData(out);
} /**
* Reads an array of employees from a scanner
* @param in the scanner
* @return the array of employees
*/
private static Student[] readData(Scanner in)
{
// retrieve the array size
int n = in.nextInt();
in.nextLine(); // consume newline Student[] employees = new Student[n];
for (int i = 0; i < n; i++)
{
employees[i] = new Student();
employees[i].readData(in);
}
return employees;
}
}

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

  • Scanner 类实例化的时候需要一个InputStream流作为参数,Scanner 的close就是关闭InputStream流的。

2.缓冲流

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

2.2 分析BufferedReader与Scanner代码,比较两者读取数据有何不同。

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

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

3.字符编码

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

Java十二周总结

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

参考:InputStreamReaderTest.java与教学PPT

dst=new String(src,"GBK").getBytes("UTF-8")

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

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

Java十二周总结

Java十二周总结

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

Java十二周总结

8.正则表达式

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

package Week12;
import java.util.regex.*;
import java.util.Scanner; public class Main {
public static void main(String[] arge){
Scanner in=new Scanner(System.in);
while(true){
String str=in.nextLine();
//String str="For my money, the important thing ";
String regEx="^(0|[1-9][0-9]*)$";
boolean result=Pattern.compile(regEx).matcher(str).find();
System.out.println(result);
} }
}

Java十二周总结

  • 0: 0
  • |:或者
  • []:默认一个字符长度
  • 0-9:数字
  • *:匹配前面的子表达式任意次
  • $:匹配输入字符串的结束位置
  • static Pattern compile(String regex) ,将给定的正则表达式编译并赋予给Pattern类
  • String pattern() ,返回该Patter对象所编译的正则表达式。

3. 码云及PTA

3.1. 码云代码提交记录

Java十二周总结