2016-04-14 19 views
0

Kullanıcının sahip olduğu bir Kişi Listesi belgesini yöneten bir program yazmaya çalışıyorum. Program, kullanıcıyı almak istedikleri dosyaya yönlendirmeli, ardından kişi listesini görüntüleme, bir kişi ekleme, bir kişiyi kaldırma ve ilgili kişinin güncel sürümünü kaydetme seçeneklerini sunmalıdır. Kodumdaki her şey dosyayı çıkarmaya çalışana kadar çalışır. Bir "FileNotFoundException (sistemde çok fazla dosya)" alıyorum. İhtiyaç duyulan iseBir Java programında varolan bir dosyayı nasıl yeniden yazarım?

import java.io.*; 
import java.util.Scanner; 
import java.util.TreeMap; 

public class ContactList { 

public static void main (String [] args) throws IOException 
{ 
    String contactFile = null; 
    Scanner input = new Scanner(System.in); 
    System.out.print("Enter name of contact file: "); 
    contactFile = input.next(); 

    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(contactFile))); 
    TreeMap< String, String > contacts = new TreeMap< String, String >(); 
    Contact contact = new Contact(); 

    br.close(); 



    menu(); 
    int userChoice = input.nextInt(); 

    while (userChoice != 4) 
    { 
     if (userChoice == 1) 
     { 

      menu(); 
      userChoice = input.nextInt(); 
     } 

     if (userChoice == 2) 
     { 
      System.out.print("Number of contacts to add: "); 
      int numContacts = input.nextInt(); 

      for (int i = 0; i < numContacts; i++) 
      { 

       contact.setName(input.nextLine()); 
       System.out.print("Enter contact's name (Last name, First name): "); 
       contact.setName(input.nextLine()); 
       contact.setPhoneNumber(input.nextLine()); 
       System.out.print("Enter contact's phone number (xxx-xxx-xxxx): "); 
       contact.setPhoneNumber(input.nextLine()); 
       contact.setEmail(input.nextLine()); 
       System.out.print("Enter contact's email (ex. [email protected]): "); 
       contact.setEmail(input.nextLine()); 

       contacts.put(contact.getName(), contact.remainingInfo()); 
      } 

      menu(); 
      userChoice = input.nextInt(); 
     } 

     if (userChoice == 3) 
     { 
      System.out.print("Enter name of contact you wish to remove (Last name, First name): "); 
      contacts.remove(input.nextLine()); 

      menu(); 
      userChoice = input.nextInt(); 
     } 

    } 

    if (userChoice == 4) 
    { 
     PrintWriter outFile = new PrintWriter(contactFile); 
     outFile.print(contacts.entrySet()); 
    } 

} 

public static void menu() 
{ 
    System.out.println("1 Display Contact List"); 
    System.out.println("2 Add a Contact"); 
    System.out.println("3 Remove a Contact"); 
    System.out.println("4 Save Contact List and Exit"); 
    System.out.print("Command: "); 
} 
} 

Ve İletişim sınıfı: Aşağıda benim şimdiye kadar kodudur

public class Contact { 
private String name; 
private String phoneNumber; 
private String email; 

public void setName(String name) { 
    this.name = name; 
} 

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

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

public String getName() 
{ 
    return name; 
} 

public String remainingInfo() 
{ 
    return phoneNumber + " " + email; 
} 

} 

bu dosyayı, değişiklik yapmak, bir dosya almak üzerine yazmak için bir yolu var mı ve çıkış/kaydetme o? Düzenlenmiş dosyanın aynı konuma çıkarılmasının bunun üzerine yazılacağını, ancak görünüşe göre değil.

Güncelleme: Ben olsun hata mesajı tam olarak okur:

Exception in thread "main" java.io.FileNotFoundException: /Users/jesbarba/Desktop/Contacts.txt (Too many open files in system) 
at java.io.FileOutputStream.open0(Native Method) 
at java.io.FileOutputStream.open(FileOutputStream.java:270) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:213) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:101) 
at java.io.PrintWriter.<init>(PrintWriter.java:184) 
at ContactList.main(ContactList.java:70) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:497) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

Bu sonraki aksi takdirde bir try-with-resources statement içine BufferedReader br ve sizin PrintWriter outFile kapatmak asla senin yüzünden sarmak gerekir intellij

+0

Dosya çıktı kodu nerede? – ControlAltDel

+0

"FileNotFoundException (sistemde çok fazla dosya)" hata mesajını mı yoksa farklı bir dilden mi çeviriyorsunuz? Eğer iseniz ve bu linux/unix ise, İngilizce mesajları almak için 'LANG = C LC_ALL = C java ContactList' çalıştırabilir misiniz? –

+0

@thatotherguy Aldığım tam hata mesajını yansıtmak için güncellenmiş soru. – Jes

cevap

0

kullanırken senin Özellikle Windows işletim sisteminde bir sorun olabilir dosyaları düzgün:

try (PrintWriter outFile = new PrintWriter(contactFile)) { 
    outFile.print(contacts.entrySet()); 
} 
+0

Bu program çalıştırdığım ilk kez çalıştı, ancak şimdi aynı değiştirilmiş dosyayla çalıştıramıyorum. Ben aynı "Bu tür dosya veya dizin yok" diyerek bu FileNotFoundException olsun. BufferedReader br'i denemede denedim. – Jes

+0

Makinenizi yeniden başlatmanız gerekiyor –

+0

Yeniden başlatma işe yaramadı. – Jes

İlgili konular