作为初学者,我正在尝试将一组信息打印到名为" Sign_in.txt"的文件中。但此代码仅创建文件,并且不打印任何内容。我做错了什么?
String nu="CUSTOMER DETAILS START";
String enu="CUSTOMER DETAILS END";
n=namefield.getText();
a=agefield.getText();
ad=adfield.getText();
s=salfield.getText();
p=phfield.getText();
d=dobfield.getText();
e=emfield.getText();
ArrayList<String> list = new ArrayList<String>();
list.add(nu);
list.add(n);
list.add(a);
list.add(ad);
list.add(s);
list.add(p);
list.add(d);
list.add(e);
list.add(u);
list.add(q);
list.add(enu);
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Sign_in.txt",true)))) {
Writer output = new BufferedWriter(out);
int l = list.size();
for(int i = 0; i<l; i++){
output.write(list.get(i).toString()+"\n");
}
output.close();
}
catch (Exception e)
{
e.printStackTrace();
}
JOptionPane.showMessageDialog(this,"Thank you for registering");
System.exit(0);
new accpage().setVisible(true);
}
答案 0 :(得分:0)
工作代码
try (PrintWriter output = new PrintWriter(
new BufferedOutputStream(
new FileOutputStream("Sign_in.txt")), true))
{
int l = list.size();
for (int i = 0; i < l; i++) {
output.println(list.get(i).toString());
}
} catch (Exception e) {
e.printStackTrace();
}
答案 1 :(得分:0)
我会有这样的事情:
try (PrintWriter output =
new PrintWriter(new BufferedWriter(new FileWriter("Sign_in.txt"))))
{
for (String line: list) {
output.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}