【发布时间】:2016-02-22 13:49:48
【问题描述】:
因此,对于一个学校项目,我需要将用户帐户详细信息保存在一个文本文件中,如果您登录,程序必须从文本文件中读取该用户是否存在。
现在写作和阅读工作,但我的问题是文本文件的内容在我在eclipse中打开文件之前不会更新。 打开文件后,我可以看到文件中突然出现了新内容。
这是我现在的代码:
String path = context.getRealPath("/");
path = path.substring(0, path.length() - 71);
path = path + "\\v2iac14\\src\\main\\resources\\accountIDs.txt";
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fOut = new FileWriter(file, true);
fOut.append("e:" + username + ";p:" + password + ";i:" + id + ";c:-----;");
fOut.flush();
fOut.close();
以及我是如何阅读文件的:
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("accountIDs.txt");
String result = null;
try {
result = IOUtils.toString(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return result;
我已经尝试了多种写入文件的方法,但问题仍然存在..
【问题讨论】:
-
顺便说一句,除非您确定允许第三方库,否则您可能需要在学校作业中小心使用
IOUtils(来自 commons-io)。我记得,我的所有作业都禁止我使用任何第三方库。 -
没有提到那个;)