gpt4 book ai didi

java - 尽管我多次运行程序,如何才能只打印一次 pw.println 的三个语句

转载 作者:行者123 更新时间:2023-12-02 10:06:28 24 4
gpt4 key购买 nike

即使我多次运行该程序,我也只尝试打印一次 pw.println 语句。有什么解决办法吗?

public static void main(String[] args) throws IOException {
// TODO code application logic here
Scanner a=new Scanner (System.in);
String First_Name , Last_Name, Address , E_mail , Department;
int Password;
System.out.println("ENTER FIRST NAME: ");
First_Name=a.next();
System.out.println("ENTER LAST NAME: ");
Last_Name=a.next();
System.out.println("ENTER ADDRESS: ");
Address=a.next();
System.out.println("ENTER E_MAIL: ");
E_mail=a.next();
System.out.println("ENTER DEPARTMENT: ");
Department=a.next();
System.out.println("ENTER PASSWORD: ");
Password=a.nextInt();
FileWriter fw=new FileWriter("C:\\Users\\HP\\Desktop\\STUD_INFO.txt" , true);
PrintWriter pw=new PrintWriter (fw);
pw.println("\t\t\t\t\t\tSTUDENT INFORMATION");
pw.println("---------------------------------------------------------------------------------------------------------------------------------------------------");
pw.println("First_Name" + "\t\t " + "Last_Name" + "\t\t " + "Address" + "\t\t " + "E_mail" + "\t\t " + "Department" + "\t\t " + "Password");
pw.println(First_Name + "\t\t\t " + Last_Name + "\t\t " + Address + "\t\t " + E_mail + "\t " + Department + "\t\t\t " + Password);
pw.close();
}

最佳答案

正如 Crusha 指出的那样,在将输出打印到文件中之前检查该文件是否存在。例如:

File f = new File("C:\\Users\\HP\\Desktop\\STUD_INFO.txt");
FileWriter fw=new FileWriter("C:\\Users\\HP\\Desktop\\STUD_INFO.txt" , true);
PrintWriter pw=new PrintWriter (fw);
if (!f.exists()) {
pw.println("\t\t\t\t\t\tSTUDENT INFORMATION");
pw.println("---------------------------------------------------------------------------------------------------------------------------------------------------");
pw.println("First_Name" + "\t\t " + "Last_Name" + "\t\t " + "Address" + "\t\t " + "E_mail" + "\t\t " + "Department" + "\t\t " + "Password");
}
pw.println(First_Name + "\t\t\t " + Last_Name + "\t\t " + Address + "\t\t " + E_mail + "\t " + Department + "\t\t\t " + Password);

关于java - 尽管我多次运行程序,如何才能只打印一次 pw.println 的三个语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55307859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com