gpt4 book ai didi

java - 创建并写入文件

转载 作者:行者123 更新时间:2023-11-30 06:47:36 25 4
gpt4 key购买 nike

该程序提示用户输入要写入联系人列表的文件的名称。他们输入姓名、电话和电子邮件等信息,这些信息会打印到屏幕上并写入文件。输入的信息打印到屏幕上并创建文件,但会写入注释。我错过了什么?

import java.util.*;
import java.io.*;

public class ContactInfo
{
public static void main(String[] args) throws IOException
{
try
{
int Command = 0;
String FileName = "";
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);

System.out.print("Enter the name of the file for the contact list: ");

FileName = input.nextLine();
System.out.println();

File OutPutFile = new File(FileName);


PrintWriter ContactList = new PrintWriter(OutPutFile);

ContactList.format("%15s%12s%20s\n", "Name", "Phone Number", "Email Address" );
ContactList.format("%15s%12s%20s\n", "------------", "------------", "---------------------" );

String First_Name = "";
String Last_Name = "";
String Remove_Last_Name = "";
String Phone_Number = "";
String Email_Address = "";

//Information InformationObject = new Information(String,String,String,String);
TreeMap<String, Information> Entries = new TreeMap<String, Information>();



do
{
System.out.println();
PrintMenu();
System.out.println();
System.out.print("Enter a command ");
Command = input2.nextInt();
System.out.println();

if (Command == 1)
{

System.out.print("Enter the person's first name: ");
First_Name = input.nextLine();
System.out.println();

System.out.print("Enter the person's last name: ");
Last_Name = input.nextLine();
System.out.println();

System.out.print("Enter the person's phone number: ");
Phone_Number = input.nextLine();
System.out.println();

System.out.print("Enter the person's email address: ");
Email_Address = input.nextLine();
System.out.println();

Information InformationObject = new Information(First_Name, Last_Name, Phone_Number, Email_Address);

Entries.put(InformationObject.getLastName(), InformationObject );


}

if (Command == 2)
{
System.out.print("Enter the last name of the person whom you want to delete from the list: ");
Remove_Last_Name = input.nextLine();
System.out.println();

Entries.remove(Remove_Last_Name);
}

if (Command == 3)
{
for (Map.Entry Document : Entries.entrySet() )
{
Information IO = Entries.get( Document.getKey());
System.out.println( IO.getLastName() + ", " + IO.getFirstName() + " " + IO.getPhoneNumber() + " " + IO.getEmail() );
ContactList.format("%15s%12s%20s\n", IO.getFirstName() + IO.getLastName(), IO.getPhoneNumber(), IO.getEmail() );
}



}






}
while(Command != 4);



}
catch(Exception e)
{
System.out.println("Invalid input");
}







}

public static void PrintMenu()
{
System.out.println();
System.out.println("Add a contact: <1>");
System.out.println("Delete a contact: <2>");
System.out.println("Display contact list: <3>");
System.out.println("Exit: <4>");
System.out.println();
}



}






class Information
{

private String FirstName;
private String LastName;
private String PhoneNumber;
private String Email;

public Information(String FirstName, String LastName, String PhoneNumber, String Email)
{
this.FirstName = FirstName;
this.LastName = LastName;
this.PhoneNumber = PhoneNumber;
this.Email = Email;

}

public void setFirstName(String FirstName)
{
this.FirstName = FirstName;
}

public void setLastName(String LastName)
{
this.LastName = LastName;
}

public void setPhoneNumber(String PhoneNumber)
{
this.PhoneNumber = PhoneNumber;
}

public void setEmail(String Email)
{
this.Email = Email;
}

public String getFirstName()
{
return FirstName;
}

public String getLastName()
{
return LastName;
}

public String getPhoneNumber()
{
return PhoneNumber;
}

public String getEmail()
{
return Email;
}

public String PrintInformation()
{
return LastName + ", " + FirstName + " " + PhoneNumber + " " + Email;
}











}

最佳答案

您忘记在最后关闭 PrintWriter。

使用finally block 来关闭对象。

ContactList.close();

关于java - 创建并写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43422648/

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