Java 可能已经打开文件错误;停止

Java 可能已经打开文件错误;停止,java,Java,所以,当我运行这个程序时,没有异常,但是执行会停止。我输入了几行代码,看看拖把是从哪里来的。在初始执行时,它按照Customer类中给定的路径创建一个文件。一旦我执行了其中一个操作,它就不会让我通过第一个调试行。想法 申请书如下: 包javaapplication18.pkg3 import java.util.ArrayList; import java.util.Scanner; public class JavaApplication183 { /** * @param args t

所以,当我运行这个程序时,没有异常,但是执行会停止。我输入了几行代码,看看拖把是从哪里来的。在初始执行时,它按照Customer类中给定的路径创建一个文件。一旦我执行了其中一个操作,它就不会让我通过第一个调试行。想法

申请书如下: 包javaapplication18.pkg3

import java.util.ArrayList;
 import java.util.Scanner;
public class JavaApplication183 {

/**
 * @param args the command line arguments
 */
static boolean keepGoing = true;
public static void main(String[] args) {
    System.out.println("Welcome to the Customer Maintenance application");
    //keepGoing = true;
    Scanner sc = new Scanner(System.in);
    while (keepGoing){
        displayMenu();
        String userChoice = getRequiredString("Enter a command: ", sc);
        System.out.println("DEBUG LINE 1");


        CustomerTextFile textFile = new CustomerTextFile();
        System.out.println("DEBUG LINE 2");
        performAction(userChoice, textFile);
        System.out.println("DEBUG LINE 3");

    }
    // TODO code application logic here
}

public static void displayMenu() {
    System.out.println();
    System.out.println("COMMAND MENU");
    System.out.println("list    - List all customers");
    System.out.println("add     - Add a customer");
    System.out.println("del     - Delete a customer");
    System.out.println("help    - Show this menu");
    System.out.println("exit    - Exit this application");
    System.out.println();
}



public static void performAction(String choice, CustomerTextFile textFile){
    Scanner sc = new Scanner(System.in);
    switch (choice.toLowerCase()) {
        case "list":    
            //action
            ArrayList<Customer> currentList = textFile.getCustomers();
            for (Customer c : currentList) {
                System.out.print(c.getEmail() + "\t");
                System.out.print(c.getFirstName() + "\t");
                System.out.println(c.getLastName());
                }
            break;
        case "add":
            String email = getRequiredString("Enter customer email address:", sc);
            String firstName = getRequiredString("Enter first name:", sc);
            String lastName = getRequiredString("Enter last name:", sc);
            Customer c = new Customer(email, firstName, lastName);
            textFile.addCustomer(c);
            System.out.println(firstName + lastName + " was added to the database.");
            break;
        case "del":

            String deleteUserByEmail = getRequiredString("Enter customer email to delete:", sc);
            Customer delCustomer = textFile.getCustomer(deleteUserByEmail);
            textFile.deleteCustomer(delCustomer);
            break;
        case "help":
            //displayMenu();
            break;
        case "exit":
           keepGoing = false;//exit();
           break;
        default:
            System.out.println("You entereed something not in the list. Please try again.");
            System.out.println();
    }
}


public static boolean exit(){
    System.out.println("Exit");
    return false;
    }

public static String getRequiredString(String prompt, Scanner sc) {
String s = "";
boolean isValid = false;
while (isValid == false) {
    System.out.print(prompt);
    s = sc.nextLine();
            if (s.equals("")) 
                System.out.println("Error! This entry is required. Try again.");
            else 
                isValid = true;
            }
    return s;
}

}
import java.util.ArrayList;
导入java.util.Scanner;
公共类JavaApplication183{
/**
*@param指定命令行参数
*/
静态布尔值keepGoing=true;
公共静态void main(字符串[]args){
System.out.println(“欢迎使用客户维护应用程序”);
//持续=正确;
扫描仪sc=新的扫描仪(System.in);
同时(继续){
显示菜单();
String userChoice=getRequiredString(“输入命令:”,sc);
System.out.println(“调试行1”);
CustomerTextFile textFile=新CustomerTextFile();
System.out.println(“调试行2”);
性能(用户选择、文本文件);
System.out.println(“调试行3”);
}
//此处的TODO代码应用程序逻辑
}
公共静态无效显示菜单(){
System.out.println();
System.out.println(“命令菜单”);
System.out.println(“列表-列出所有客户”);
System.out.println(“添加-添加客户”);
System.out.println(“删除-删除客户”);
System.out.println(“帮助-显示此菜单”);
System.out.println(“退出-退出此应用程序”);
System.out.println();
}
公共静态无效性能(字符串选择,CustomerTextFile文本文件){
扫描仪sc=新的扫描仪(System.in);
开关(choice.toLowerCase()){
案例“列表”:
//行动
ArrayList currentList=textFile.getCustomers();
对于(客户c:currentList){
System.out.print(c.getEmail()+“\t”);
System.out.print(c.getFirstName()+“\t”);
System.out.println(c.getLastName());
}
打破
案例“添加”:
字符串email=getRequiredString(“输入客户电子邮件地址:”,sc);
String firstName=getRequiredString(“输入名字:”,sc);
String lastName=getRequiredString(“输入姓氏:”,sc);
客户c=新客户(电子邮件、名字、姓氏);
textFile.addCustomer(c);
System.out.println(firstName+lastName+“已添加到数据库中。”);
打破
案例“del”:
String deleteUserByEmail=getRequiredString(“输入要删除的客户电子邮件:”,sc);
Customer delCustomer=textFile.getCustomer(deleteUserByEmail);
textFile.deleteCustomer(delCustomer);
打破
案例“帮助”:
//显示菜单();
打破
案例“退出”:
keepGoing=false;//退出();
打破
违约:
System.out.println(“您输入了列表中没有的内容,请再试一次。”);
System.out.println();
}
}
公共静态布尔出口(){
System.out.println(“退出”);
返回false;
}
公共静态字符串getRequiredString(字符串提示,扫描程序sc){
字符串s=“”;
布尔值isValid=false;
while(isValid==false){
系统输出打印(提示);
s=sc.nextLine();
如果(s.等于(“”)
System.out.println(“错误!需要此条目。请重试”);
其他的
isValid=true;
}
返回s;
}
}
以下是CustomerTextFile类:

package javaapplication18.pkg3;

import java.io.*;
import java.nio.file.*;
import java.util.ArrayList;

public class CustomerTextFile implements CustomerDAO{
    private ArrayList<Customer> customers = null;
    private Path customersPath = null;
    private File customersFile = null;

public CustomerTextFile(){

    customersPath = Paths.get("customers.txt");
    customersFile = customersPath.toFile();
    customers = this.getCustomers();

}
@Override
public Customer getCustomer(String emailAddress) {
    for (Customer c : customers) {
        if (c.getEmail().equals(emailAddress))
                return c;
    }
    return null;

    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public ArrayList<Customer> getCustomers() {
    if (customers != null)
        return customers;

    customers = new ArrayList<>();

    if (!Files.exists(customersPath)) {
        try {
            Files.createFile(customersPath);
        }
        catch (IOException e) {
            return null;
        }
    }

    if (Files.exists(customersPath)) {
        try (BufferedReader in = new BufferedReader(new FileReader(customersFile))){
            String line = in.readLine();
            while(line != null) {
                String[] columns = line.split("\t");
                String email = columns[0];
                String firstName = columns[1];
                String lastName = columns[2];

                Customer c = new Customer(email, firstName, lastName);
                customers.add(c);

            }

        }
        catch (IOException e) {
            System.out.println(e);
            return null;
        }
    }
    return customers;
}

@Override
public boolean addCustomer(Customer c) {
    customers.add(c);
    return this.saveCustomers();
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean updateCustomer(Customer c) {
    Customer oldCustomer = this.getCustomer(c.getEmail());
    int i = customers.indexOf(oldCustomer);
    customers.remove(i);

    customers.add(i, c);

    return this.saveCustomers();
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean deleteCustomer(Customer c) {
    customers.remove(c);
    return this.saveCustomers();
}

private boolean saveCustomers() {
    try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(customersFile)))){

        for (Customer customer : customers) {
            out.print(customer.getEmail() + "\t");
            out.print(customer.getFirstName() + "\t");
            out.println(customer.getLastName());

        }
        out.close();
        return true;

    }
    catch (IOException e) {
        return false;
    }



}
PackageJavaApplication18.pkg3;
导入java.io.*;
导入java.nio.file.*;
导入java.util.ArrayList;
公共类CustomerTextFile实现CustomerDAO{
私有ArrayList客户=null;
私有路径customersPath=null;
私有文件customersFile=null;
公共CustomerTextFile(){
customersPath=path.get(“customers.txt”);
customersFile=customersPath.toFile();
customers=this.getCustomers();
}
@凌驾
公共客户getCustomer(字符串电子邮件地址){
用于(客户c:客户){
如果(c.getEmail().equals(emailAddress))
返回c;
}
返回null;
//抛出新的UnsupportedOperationException(“尚未受支持”);//若要更改生成的方法体,请选择“工具”“模板”。
}
@凌驾
公共阵列列表getCustomers(){
如果(客户!=null)
返回客户;
客户=新的ArrayList();
如果(!Files.exists(CustomerPath)){
试一试{
Files.createFile(CustomerPath);
}
捕获(IOE异常){
返回null;
}
}
if(Files.exists(customersPath)){
try(BufferedReader in=new BufferedReader(new FileReader(customersFile))){
String line=in.readLine();
while(行!=null){
String[]columns=line.split(“\t”);
字符串email=columns[0];
String firstName=列[1];
字符串lastName=列[2];
客户c=新客户(电子邮件、名字、姓氏);
加上(c);
}
}
捕获(IOE异常){
系统输出打印ln(e);
返回null;
}
}
返回客户;
}
@凌驾
公共客户(客户c){
加上(c);
返回这个。saveCustomers();
//抛出新的UnsupportedOperationException(“尚未受支持”);//若要更改生成的方法体,请选择“工具”“模板”。
}
@凌驾
公共布尔更新计数器(客户c){
Customer oldCustomer=this.getCustomer(c.getEmail());
int i=客户。indexOf(旧客户);
客户。删除(i);
添加(i,c);
返回这个。saveCustomers();
//抛出新的UnsupportedOperationException(“尚未受支持”);//若要更改生成的方法体,请选择“工具”“模板”。
}
@凌驾
公共布尔
Scanner sc = new Scanner(System.in);
String userChoice;
do {
    displayMenu();
    userChoice = sc.nextLine(); //takes in the entire lien you type in
    System.out.println("DEBUG LINE 1");


    CustomerTextFile textFile = new CustomerTextFile();
    System.out.println("DEBUG LINE 2");
    performAction(userChoice, textFile);
    System.out.println("DEBUG LINE 3");
} while(keepGoing);