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();
}
}
}
This code writes the contents of a directory (I used a MAP in the directory class) to a .txt file. The above thread is being executed in the main thread. Even debugging didn't help. Please explain why the file remains empty though being created.