2015-04-05 23 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

相关问题