新文件读/写Java

时间:2015-09-17 21:18:17

标签: java filenotfoundexception

我正在尝试创建一个从.txt文件中读取帐户信息的银行程序。它应该问你想做什么,获取你的账号,找出你想要提取/存入的金额,然后在文本文件上进行更改。

我在使用FileNotFoundException时遇到了一些麻烦。我之前从未使用过它而且我不确定如何捕获"它。我搜索了一下,发现了一些对我来说没有意义的答案,关于"尝试"声明和我不打算使用的FileReader。我不知道他们做了什么或他们会去哪里。你可以给我指导吗? (请原谅半生不熟的代码,我无法运行它以查看在修复此问题之前有效的方法)

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintWriter;


public class Main {

public static void main(String[] args){
    option();
    }

public static void option(){
    System.out.println("Enter the number for your selection \n 1) Deposit \n 2) Withdraw \n 0) Exit");
    Scanner keyboard = new Scanner(System.in);
    int input = keyboard.nextInt();
    if(input==0) close();
    System.out.println("Please enter your account number:");
    String account = keyboard.nextLine();
    System.out.println("Please enter the amount for your transaction:");
    String amount = keyboard.nextLine();

    if(input==1) deposit(account, amount);          //if 1 they want a deposit
    //else if(input==2) withdraw(account, amount);  //if 2 they want a deposit
    else if(input==0) close();                      //if 0 end the transaction 
    else {
        System.out.println("That is not a valid option, try again.");   //if a wrong character is entered, restart
        option();
    }
}

public static void deposit(String account, String amount) throws FileNotFoundException {
    int i=-2;
    Scanner file = new Scanner(new File("info.txt"));
    String line = file.nextLine();
    String[] tokens = line.split(" ");
    do {
        i++;
        i++;
    }
    while (account!=tokens[i]);

    int ballance = Integer.parseInt(tokens[i]);
    int deposit = Integer.parseInt(amount);
    int updated = (ballance+deposit);
    tokens[i] = Integer.toString(updated);

    System.out.println("Your new ballance is $"+tokens[i]);
    PrintWriter fileOut = new PrintWriter("info.txt");
    fileOut.close();
    file.close();
}

/*public static void withdraw(String account, String amount) throws FileNotFoundException {
    int i=0;
    Scanner file = new Scanner(new File("info.txt"));
    String line = file.nextLine();
    String[] tokens = line.split(" ");
    if (account==tokens[i]){
        i++;
        int ballance = Integer.parseInt(tokens[i]);
        int deposit = Integer.parseInt(amount);
        int updated = (ballance+deposit);
        tokens[i] = Integer.toString(updated);
        } 
    System.out.println("withdraw"); 
}
*/
public static void close(){
    System.out.println("Thank You for banking with us!");
    option();
}

}

2 个答案:

答案 0 :(得分:1)

我不知道我是否理解你,但你的意思是你不知道如何使用try / catch构造?

最好的方法是阅读教程:

https://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html

快速添加代码:

deal = Deal.find(5)
prizes = deal.prizes

} }

答案 1 :(得分:0)

说到Exceptions,有两种方法可以处理它。一种是抛出异常(在将throws添加到每个调用方法main之后)。这会停止之后完成的任何代码。

或者您可以捕获异常并自行处理。这允许仍然可以调用代码。

作为一个例子,我使用了deposit方法来读取File以抛出并捕获异常。首先调用catch,因为它允许在之后调用代码。输出也在下面。

public static void main(String[] args) throws FileNotFoundException {
    catchDeposit("","");
    System.out.println("print after catchDepsoit");
    throwDeposit("","");
    System.out.println("print after throwDepsoit");
}
public static void catchDeposit(String account, String amount) {
    int i=-2;
    try {
        Scanner file = new Scanner(new File("info.txt"));
    } catch (FileNotFoundException e) {
        System.out.println("File not found");
    }
}
public static void throwDeposit(String account, String amount) throws FileNotFoundException{
    int i=-2;
    Scanner file = new Scanner(new File("info.txt"));

输出

File not found
print after catchDepsoit
Exception in thread "main" java.io.FileNotFoundException: info.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at Random.throwDeposit(Random.java:25)
    at Random.main(Random.java:10)

希望这会有所帮助。