【发布时间】:2016-02-06 07:27:21
【问题描述】:
所以我正在编写代码,作为一种用于登录系统的文件系统,预先添加了两个帐户,两个帐户都是管理员。他们可以创建帐户。所有帐户都存储在其用户名的数组列表和密码的数组列表中。我正在尝试这样做,以便当我导出此应用程序时,当管理员添加到数组列表时,会将信息发送到 txt 文件,以便下次运行程序时,程序将加载任何帐户txt 文件。我已经测试了帐户的加载,效果很好,但我似乎无法写入 txt 文件。我从这个站点以这种方法获取了这段代码,但我不确定我做错了什么:
public static void writeFileContnet(){
String path = "C:\\Users\\DJANDEK\\Desktop\\LogInPro\\info.txt";
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(path, true));
if(!loadFileAsString(path).equals("")){
writer.newLine();
}
System.out.println(Account.accounts.get(Account.accounts.size()-1));
writer.write(Account.accounts.get(Account.accounts.size()-1));
System.out.println(breaker);
writer.write(breaker);
System.out.println(Account.passwords.get(Account.passwords.size()-1));
writer.write(Account.passwords.get(Account.passwords.size()-1));
writer.write(breaker);
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String line;
try {
while((line = in.readLine()) != null)
{
System.out.println("it prints this");
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
对于那些想知道 println 的人,控制台显示: 鲍勃; 1Q2W; BobsPass (信息正确)。 我在控制台中没有错误。为了回答重复的问题,他被告知使用文件,而不是如何使用文件。如果我的 txt 文件中已经有数据,Reader 会读取并 s.o.p 它。
【问题讨论】:
标签: java file bufferedreader bufferedwriter