我试图将一些信息附加到文本文件中,但该文件仅显示最后写入的元素。
有许多Engineer
s,但它仅将读取的最后一个元素打印到文件中。
例如:
Engineer e = new Engineer(firstName,surName,weeklySal);
PrintStream writetoEngineer = new PrintStream(new File ("Engineer.txt"));
//This is not append. Only print. Overwrites the file on each item.
writetoEngineer.append(e.toString() + " " + e.calculateMontly(weeklySal));
我不知道你在哪里关闭文件。我也没有看到你在读任何东西。
我假设您想附加到文件而不是每次都覆盖它。在这种情况下,您需要使用 FileOutputStream 的追加选项,因为这不是默认行为。
PrintStream writetoEngineer = new PrintStream(
new FileOutputStream("Engineer.txt", true));
BTW: e.toString() + " "
几乎与e + " "
但如果 e 为 null,则不会引发异常。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)