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
編寫一個程序,能夠根據指定目錄和文件名,搜索該目錄及子目錄下的全部文件,若是沒有找到指定文件名,則顯示無匹配,不然將全部找到的文件名與文件夾名顯示出來。