文件仍为空

时间:2013-12-10 05:22:21

标签: java file-io

public class StoreTxtFile implements Runnable {

@Override
public void run() {
    String id;
    String name;
    int age;
    int standard;
    String subject;

    Set keys = HumanDirectory.myMap.keySet();
    Iterator keyIterator = keys.iterator();

    try {
        Writer newFileWriter = new BufferedWriter
                        (new OutputStreamWriter
                        (new FileOutputStream("directory.txt"), "UTF-8"));
        while(keyIterator.hasNext()) {
            Human curHuman = HumanDirectory.myMap.get(keyIterator.next());
            id = curHuman.getId();
            name = curHuman.getName();
            age = curHuman.getAge();
            if(curHuman instanceof Student) {
                standard = ((Student) curHuman).getStandard();
                newFileWriter.write(id + " - " + name + " - " + age + " - " +  standard + "\n");
            }
            else if(curHuman instanceof Teacher) {
                subject = ((Teacher) curHuman).getSubject();
                newFileWriter.write(id + " - " + name + " - " + age + " - " +  subject + "\n");
            }
        }

    } catch (IOException ioe){
        ioe.printStackTrace();
    }
}
}

此代码将目录的内容(我在目录类中使用MAP)写入.txt文件。上面的线程正在主线程中执行。即使调试也无济于事。请解释为什么文件在创建时仍然为空。

0 个答案:

没有答案
相关问题