创造&写入文件

时间:2017-04-15 05:39:34

标签: java file-io treemap

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

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;
    }











}

2 个答案:

答案 0 :(得分:1)

你忘了在最后关闭PrintWriter。

使用finally块关闭对象。

ContactList.close();

答案 1 :(得分:0)

在此程序中,它读取用户输入的数据并将其写入contact.txt文件

$your_url = "http://myserver/widget/abc.php?method=getsomething"; //put your url here
$data['objectlist'] = file_get_contents($your_url);