2017-04-15 190 views
1

該程序提示用戶輸入要寫入聯繫人列表的文件的名稱。他們輸入名稱,電話和電子郵件等信息,並打印到屏幕上並寫入文件。輸入的信息打印到屏幕上,文件被創建,但註釋被寫入它。我錯過了什麼?創建和寫入文件

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











} 

回答

1

你忘了在最後關閉的PrintWriter。

使用finally塊關閉對象。

ContactList.close(); 
0

在這個程序中讀取用戶輸入的數據並將其寫入文件contact.txt

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.Scanner; 

public class ConsoleToFile { 

InputStream is; 

public static void main(String[] args) throws IOException { 

    ArrayList<String> al = new ArrayList<String>(); 

    File file = new File("contact.txt"); 

    System.out.println(file); 
    FileOutputStream fout = new FileOutputStream(file); 


    Scanner sc = new Scanner(System.in); 
    System.out.println("enter ur name"); 
    String name = sc.nextLine(); 
    al.add(name); 
    System.out.println("enter id"); 
    String id = sc.nextLine(); 
    al.add(id); 
    System.out.println("enter phone"); 
    String p = sc.nextLine(); 
    al.add(p); 

    Iterator<String> i = al.iterator(); 

    while (i.hasNext()) { 
     String s = (String) i.next() + System.lineSeparator(); 
     System.out.println(s); 
     byte b[] = s.getBytes(); 

     fout.write(b); 
    } 
    fout.close(); 
    } 


}