1.1 以你喜欢的方式(思惟导图或其余)概括总结多流与文件相关内容。javascript
使用流与文件改造你的图书馆管理系统或购物车。java
数据的格式:主要是采用了字节流FileInputStream,再用ObjectInputStream打包。正则表达式
Serializable。
缘由:ObjectOutputStream
提供readObject()
和writeObject()
来把数据读入对象和将对象写到目的地,能够被这两种方法处理的对象,必须操做java.io.Serializable接口。类:File
、FileOutputStream
、ObjectOutputStream
。缘由:File用来建立新文件名和实例化一个文件,而fos = new FileOutputStream(file);//把数据写入到file文件中 oos = new ObjectOutputStream(fos);//打包file
编程
public void writeToFile()//把数据写入文件中 { try { file = new File("Books_info.obj");//建立新文件名为Books_info.obj file.delete(); file.createNewFile(); FileOutputStream fos = null; ObjectOutputStream oos = null; fos = new FileOutputStream(file);//把数据写入到file文件中 oos = new ObjectOutputStream(fos);//打包file oos.writeObject(allBooks);//写入allBooks里 oos.flush();// oos.close(); } catch(Exception e) { System.out.println(e); } } @SuppressWarnings("unchecked") public void readFromFile()//把数据从文件中读出来 { try { file = new File("Books_info.obj"); FileInputStream fis = null; ObjectInputStream ois = null; fis = new FileInputStream(file); ois = new ObjectInputStream(fis); allBooks.clear(); allBooks = (HashMap<Integer,Book>) ois.readObject(); ois.close(); } catch(Exception e) { System.out.println(e); } }
public void writeToFile()//把读者写进文件 { try { file = new File("Readers_info.obj"); file.delete(); file.createNewFile(); FileOutputStream fos = null; ObjectOutputStream oos = null; fos = new FileOutputStream(file); oos = new ObjectOutputStream(fos); oos.writeObject(allReaders); oos.flush(); oos.close(); } catch(Exception e) { System.out.println(e); } } @SuppressWarnings("unchecked") public void readFromFile()//从文件中读出全部读者 { try { file = new File("Readers_info.obj"); FileInputStream fis = null; ObjectInputStream ois = null; fis = new FileInputStream(file); ois = new ObjectInputStream(fis); allReaders.clear(); allReaders = (Map<String, Reader>) ois.readObject(); ois.close(); } catch(Exception e) { System.out.println(e); } }
public void borrowBookSystem(Book book)//借书系统 { if(allBooks.containsKey(book.getId())) { Book temp = allBooks.get(book.getId()); temp.setNum(temp.getNum() - 1); allBooks.replace(book.getId(), temp); writeToFile(); } } public void returnBookSystem(Book book)//还书系统 { book.setNum(book.getNum() + 1); allBooks.replace(book.getId(), book); writeToFile(); } public boolean addBook(Book book)//添加书 { if(!allBooks.containsKey(book.getId())) { allBooks.put(book.getId(), book); writeToFile();//写入文件 return true; } else return false; }
须要将每周的代码统计状况融合到一张表中。
编辑器
周次 | 行数 | 新增行数 | 文件数 | 新增文件数 |
---|---|---|---|---|
1 | 91 | 91 | 5 | 5 |
2 | 504 | 413 | 18 | 13 |
3 | 1092 | 588 | 28 | 10 |
5 | 1158 | 129 | 34 | 6 |
6 | 1539 | 381 | 40 | 6 |
7 | 2023 | 484 | 49 | 9 |
8 | 2477 | 454 | 57 | 8 |
9 | 2709 | 232 | 63 | 6 |
10 | 3156 | 447 | 70 | 7 |
11 | 3531 | 375 | 79 | 9 |
12 | 4083 | 552 | 91 | 12 |
13 | 4850 | 106 | 15 |
将Student对象(属性:int id, String name,int age,double grade)写入文件student.data、从文件读出显示。函数
分析:在每行中,id为1字节,name为4字节,age为2字节,grade为4字节,三个分隔符为3字节,最后加上换行符2字节,因此一行一共16字节,三行则为48字节。学习
printWrite是将数据写在缓冲区里,没有close()就会致使数据在缓冲区里丢失。则找不到数据了。
参考:本题具体要求见流与文件实验任务书-题目1-2.1
参考代码:TextFileTest.java测试
参考:InputStreamReaderTest.java与教学PPTthis
\\陈锦霞201621123061 import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; //4.1 参考DataStream目录相关代码,尝试将三个学生对象的数据写入文件,而后从文件读出并显示 public class Student1 { private int id; private String name; private int age; private double grade; public Student1(){ } public Student1(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; } public static void main(String[] args) throws IOException{ FileOutputStream fos=new FileOutputStream(new File("Student.txt"));//写入文件 DataOutputStream dos=new DataOutputStream(fos); Student[] stu=new Student[3]; stu[0]=new Student(1,"张三",19,65.0); stu[1]=new Student(1,"李四",19,75.0); stu[2]=new Student(1,"王五",20,85.0); try{ for(int i=0;i<3;i++){ dos.writeInt(stu[i].id); dos.writeUTF(stu[i].name); dos.writeInt(stu[i].age); dos.writeDouble(stu[i].grade); } }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("id:"+dis.readInt() ); System.out.println("name:"+dis.readUTF() ); System.out.println("age:"+dis.readInt() ); System.out.println("grade:"+dis.readDouble() ); } }finally{ dis.close(); } }}
由于编码方式不一样。编码
编写public static List
编写一个程序,能够根据指定目录和文件名,搜索该目录及子目录下的全部文件,若是没有找到指定文件名,则显示无匹配,不然将全部找到的文件名与文件夹名显示出来。