java - 如何在 Java 中正确使用 try-catch-finally block ?

标签 java try-catch fstream instantiation try-catch-finally

使用 finally block 关闭我所在的文件的正确方法是什么:event.dat。如果你能帮助我理解这一点,我将不胜感激。我真的花了 4 个小时来移动东西、玩代码并在线搜索答案,但无济于事。没有那部分代码工作得很好,但我需要知道它在未来的实例中是如何工作的。谢谢你,有一个美好的一天!

我遇到这个 block 的问题:

          finally{
              fstream.close();        
          } 

位于以下代码中:

    String answer;
    String letter;
    String inType = "";
    double inAmount = 0;
    double amount;

    description();
    GironEventClass newInput = new GironEventClass();
    try{
        File infile = new File("event.dat");
        Scanner fstream = new Scanner(infile);

        System.out.println("File Contents ");

        while(fstream.hasNext())
        {
            inAmount = fstream.nextInt();
            inType = fstream.next();

            try{
                newInput.donations(inType, inAmount);

            }
            catch(IllegalArgumentException a){
                System.out.println("Just caught an illegal argument exception. ");
            }  
           finally{
                   fstream.close();        
          }  
        }

        System.out.println("Total Sales: " + newInput.getSale());
        System.out.println("Donations: " + newInput.getDonated());
        System.out.println("Expenses: " + newInput.getExpenses());



    }

    catch (FileNotFoundException e){
        System.out.println("\nEvent.dat could not be opened. ");
    }

    do{
        System.out.print("Are there any more items to add that were not in the text file? (Type 'Y' or 'N')");
        answer = keyboard.next();
        if (("Y".equals(answer)) || ("y".equals(answer)))
        {
            letter = inLetter();
            amount = inAmount();

            newInput.donations(letter, amount);
        }

    }while (("Y".equals(answer)) || ("y".equals(answer)));

    newInput.display();
}

public static String inLetter(){
    Scanner keyboard = new Scanner(System.in);
    String result;
    String resultTwo;

    System.out.println("T = Tiket Sales");
    System.out.println("D = Donations");
    System.out.println("E = Expenses");
    System.out.print("Please input an identifier ");
    result = keyboard.nextLine();
    resultTwo = result.toUpperCase();

    return resultTwo;    
}

public static double inAmount(){
    Scanner keyboard = new Scanner(System.in);
    double result;

    System.out.println("Please input an amount ");
    result = keyboard.nextInt();

    if(result <= 0.0){
        System.out.print("Please input a positive and non-zero amount ");
        result = keyboard.nextInt();
    }

    return result;
}

public static void description(){
    System.out.println("The program will ask you what amount is being spent on what.");
    System.out.println("    ex: expenses, ticket sales, and profts.");
    System.out.println("This program will help determine whether the event generated or lost money.");      
}

最佳答案

扫描仪应该是这样工作的:

while scanner has object
   read them ( one object per method's call) 
when objects are done
   close the reader.

您的问题是当 while 结论为真时您使用了 close 函数。所以你应该把它放在 while 循环之外

关于java - 如何在 Java 中正确使用 try-catch-finally block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32974843/

相关文章:

java - hibernate 查询: new DTO clause not working

java - 尝试使用准备好的语句删除元组

java - 什么是有用的抽象/契约来协助 Builder 模式构建 MVC UI?

php - 在 mysqli 中尝试/捕获

c++ - 如何将数据写入单独的行中的每个文件?

Java 时间(20 月 29 日星期三 :56:47 +0000 2012) to epoch

c++ - 在 try catch block 中返回 catch 是不是很糟糕?这是一个好习惯

c++ - 在 Codegear CBuilder 2007 Forms 中捕获异常

c++ - 为什么我的 fstream 访问不能编译?

c++ - 使用分隔符一个空格和一个分号读取文件