如何在Java程序中重写现有文件?

时间:2016-04-14 18:27:36

标签: java

我正在尝试编写一个程序来管理用户拥有的联系人列表文档。程序应提示用户输入要导入的文件,然后为他们提供显示联系人列表,添加联系人,删除联系人以及保存当前联系人版本的选项。我的代码中的所有内容都可以运行,直到我尝试输出文件。我收到“FileNotFoundException(系统中的文件太多)”。以下是我目前的代码:

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. johndoe@gmail.com): ");
                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: ");
}
}

如果需要,可以使用Contact类:

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

}

有没有办法导入文件,进行更改,覆盖该文件,并输出/保存?我认为将编辑后的文件输出到同一位置会覆盖它,但显然不会。

更新:我收到的确切错误消息为:

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)

这是使用intelliJ

的时候

1 个答案:

答案 0 :(得分:0)

您需要将BufferedReader brPrintWriter outFile包装成try-with-resources statement,因为否则您永远不会正确关闭文件,这可能是一个问题,尤其是在Windows操作系统上:

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