2015-04-05 24 views
0

這是我必須使用的示例。我花了大約30分鐘,但沒有找到關於如何使用這門課的全面內容。Java使用修改的DataInputStreams讀取/寫入

讓我解釋一下我們需要處理的事情。有一個數據庫類存儲鏈接列表中的「學生」數據。從主要方法中,我們使用數據庫類中的add()方法將學生添加到列表中。數據庫類有寫和讀所述文件的方法,但唉,我不明白現在要做什麼。說明指出:

  1. 必須打開一個新的文件流。
  2. 「應該緩衝」 - 不知道這意味着
  3. 附送類StudentRecordReader應與上述緩衝工作

    public boolean readFromFile(String fileName){ 
        boolean success = false; 
        Student s = null; 
        try{ 
        //Need to open a file stream 
        File file = new File(fileName); 
        if(!file.exists()){ 
         file.createNewFile(); 
        } 
        //Should be then buffered 
        DataInputStream in = new DataInputStream(new BufferedInputStream(new   
        FileInputStream(file))); 
        StudentRecordReader r;//this should work with a buffered stream 
        r = new StudentRecordReader(in); 
        r.close(); 
        }catch(IOException e){ 
        e.printStackTrace(); 
        } 
    return success; 
    } 
    

回答

0

我覺得StudentRecordReader必須打開一個緩衝的輸入流

DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(path))) 

然後從文件中讀取學生字段,例如

String name = dis.readUTF(); 
String address = dis.readUTF(); 

然後創建基於此數據

new Student(name, address); 

然後讀取下一個學生一個學生......

+0

這是有道理的。我的問題主要是它如何與調用它的方法聯繫起來。人們如何去發送輸入流? 注意我添加了上面的讀取方法。 – TheUnknown 2015-04-05 04:43:11

+0

//之後應該緩衝行,你應該添加新的BufferedInputStream(新的FileInputStream(文件)); – 2015-04-05 04:49:04

+0

最後一個問題。如果我在 – TheUnknown 2015-04-05 04:58:13

相關問題