1. 本章学习总结
2. 书面作业
将Student对象(属性:int id, String name,int age,double grade)写入文件student.data、从文件读出显示。
Q1. 字符流与文本文件:使用 PrintWriter(写),BufferedReader(读)
1.1 生成的三个学生对象,使用PrintWriter的println方法写入student.txt,每行一个学生,学生的每个属性之间用|作为分隔。使用Scanner或者BufferedReader将student.txt的数据读出。(截图关键代码,出现学号)
1.2 生成文件大小多少?分析该文件大小
1.3 如果调用PrintWriter的println方法,但在后面不close。文件大小是多少?为什么?
1.1
//201521123055
Student[] students=new Student[3];
students[0]=new Student(1,"lin",10,1);
students[1]=new Student(2,"chen",10,1);
students[2]=new Student(3,"zhang",10,1);
OutputStream os=new FileOutputStream("student.txt");
PrintWriter pw=new PrintWriter(os);
for(int i=0;i<3;i++)
{
String str=students[i].id+"|"+students[i].name+"|"+students[i].age+"|"+students[i].grade;
pw.println(str);
}
pw.close();
os.close();
InputStream is=new FileInputStream("student.txt");
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String info=null;
info=br.readLine();
while(info!=null){
System.out.println(info);
info=br.readLine();
}
br.close();
isr.close();
is.close();
1.2
生成45字节的文件
students[0]=id(1)+name(3)+age(2)+grade(1)+"|"(3)+/n(2)
students[1]=id(1)+name(4)+age(2)+grade(1)+"|"(3)+/n(2)
students[2]=id(1)+name(5)+age(2)+grade(1)+"|"(3)+/n(2)
合计45字节
1.3
生成0字节文件,因为没有close文件,导致未保存,数据在缓冲区中丢失。
Q2. 缓冲流
2.1 使用PrintWriter往文件里写入1千万行(随便什么内容都行),然后对比使用BufferedReader与使用Scanner从该文件中读取数据的速度(只读取,不输出),使用哪种方法快?请详细分析原因?提示:可以使用junit4对比运行时间
2.2 将PrintWriter换成BufferedWriter,观察写入文件的速度是否有提升。记录两者的运行时间。试分析原因。
2.1
BufferedReader:482ms
Scanner:8672ms
差距很大,由于BufferedReader是先把数据读到缓存区然后在写到硬盘里,而Scanner是直接往硬盘些数据,所以BufferedReader的效率比Scanner高。
2.2
PrintWriter:1976ms
BufferedWriter:748ms
PrintWriter它读取到一个字节/字符,就向用户指定的路径写出去,读一个写一个,所以就慢了
BufferedWriter读取到一个字节/字符,先不输出,等凑足了缓冲的最大容量后一次性写出去,从而提高了工作效率
3. 字符编码
3.1 现有EncodeTest.txt 文件,该文件使用UTF-8编码。使用FileReader与BufferedReader将EncodeTest.txt的文本读入并输出。是否有乱码?为什么会有乱码?如何解决?(截图关键代码,出现学号)
3.2 编写一个方法convertGBK2UTF8(String src, String dst),可以将以GBK编码的源文件src转换成以UTF8编码的目的文件dst。
3.1
由于本人使用IDEA写程序,IDEA的默认编码是UTF-8,因此在IDEA下编译运行程序并无乱码。若要处理因编码类型不同导致的乱码,做如下处理即可
//201521123055
InputStream is=new FileInputStream("EncodeTest.txt");
InputStreamReader isr=new InputStreamReader(is,"utf-8");
BufferedReader br=new BufferedReader(isr);
3.2
//201521123055
public void convertGBK2UTF8(String src, String dst)throws IOException
{
BufferedReader bf=new BufferedReader(src);
OutputStream os=new FileOutputStream(dst);
PrintWriter pw=new PrintWriter(os);
String gbk=bf.readLine();
while(gbk!=null)
{
String utf=new String(gbk,"UTF-8");
pw.println(utf);
}
bf.close();
pw.close();
os.close();
}
Q4. 字节流、二进制文件:DataInputStream, DataOutputStream、ObjectInputStream
4.1 参考DataStream目录相关代码,尝试将三个学生对象的数据写入文件,然后从文件读出并显示。(截图关键代码,出现学号)
4.2 生成的文件有多大?分析该文件大小?将该文件大小和题目1生成的文件对比是大了还是小了,为什么?
4.3 使用wxMEdit的16进制模式(或者其他文本编辑器的16进制模式)打开student.data,分析数据在文件中是如何存储的。
4.4 使用ObjectInputStream(读), ObjectOutputStream(写)读写学生。(截图关键代码,出现学号) //参考ObjectStreamTest目录
4.1
//201521123055
FileOutputStream fos = new FileOutputStream("student.txt");
DataOutputStream dos = new DataOutputStream (fos);
try
{
for(int i=0;i<3;i++)
{
dos.writeInt(students[i].getId());
dos.writeInt(students[i].getAge());
dos.writeDouble(students[i].getGrade());
dos.writeUTF(students[i].getName());
}
}
finally
{
dos.close();
}
FileInputStream fis = new FileInputStream("student.txt");
DataInputStream dis = new DataInputStream(fis);
try
{
for(int i=0;i<3;i++)
{
System.out.println(dis.readInt()+"|"+dis.readInt()+"|"+dis.readDouble()+"|"+dis.readUTF());
}
}
finally
{
dis.close();
}
结果:
1|10|1.0|lin
2|10|1.0|chen
3|10|1.0|zhang
4.2
生成文件大小66字节
students[0]=id(4)+name(5)+age(4)+grade(8)
students[1]=id(4)+name(6)+age(4)+grade(8)
students[2]=id(4)+name(7)+age(4)+grade(8)
总计66字节
4.3
按照之前提供的字节数分析
0000 0001=1
0000 000A=10
3FF0 0000 0000 0000=10.0
0003 6C69 6E=lin
以此类推其他两名学生的信息
4.4
//201521123055
FileOutputStream fos = new FileOutputStream("student.txt");
ObjectOutputStream op=new ObjectOutputStream(fos);
op.writeObject(students);
op.close();
fos.close();
FileInputStream fis = new FileInputStream("student.txt");
ObjectInputStream oi=new ObjectInputStream(fis);
try {
Student[] st = (Student[]) oi.readObject();
for(Student s:st)
{
System.out.println(s);
}
}catch (ClassNotFoundException ex){
ex.printStackTrace();
}
oi.close();
fis.close();
Q5. Scanner基本概念组装对象
编写public static List
public static List<Student> readStudents(String fileName)
{
ArrayList<Student> stu=new ArrayList<Student>();
BufferedReader bf=new BufferedReader(fileName);
String str=bf.readLine();
while(str!=null);
{
String[] mes=str.split(" ");
int id=Integer.parseInt(mes[0]);
int name=mes[1];
int age=Integer.parseInt(mes[2]);
double grade=Double.parseDouble(mes[3]);
stu.add(new(id,name,age,grade));
str=bf.readLine();
}
bf.close();
return stu;
}
采用BufferedReader高效读取。
Q7. 文件操作
编写一个程序,可以根据指定目录和文件名,搜索该目录及子目录下的所有文件,如果没有找到指定文件名,则显示无匹配,否则将所有找到的文件名与文件夹名显示出来。
7.1 编写public static void findFile(String path,String filename)函数,以path指定的路径为根目录,在其目录与子目录下查找所有和filename相同的文件名,一旦找到就马上输出到控制台。(截图关键代码,出现学号)
7.1
递归查找
//201521123055
public static void findFile(String path,String filename)
{
File file=new File(path);
File[] files=file.listFiles();
for(File f:files)
{
for(int i=0;i<files.length;i++)
{
if(f.isFile())
{
if(f.getName().equals(filename)) System.out.println(f.getAbsolutePath());
}
else if(f.isDirectory())findFile(f.getAbsolutePath(),filename);
}
}
}
Q8. 正则表达式
8.1 如何判断一个给定的字符串是否是10进制数字格式?尝试编程进行验证。(截图关键代码,出现学号)
String regEx= "^(-?\d+)(\.\d+)?$";
String[] str={"-123.02","230","350sa"};
for(String s:str)
{
System.out.println(s.matches(regEx));
}
结果:
true
true
false