簡體   English   中英

Java文件讀寫新手

[英]New to file reading/writing Java

我正在嘗試創建一個銀行程序,該程序從.txt文件讀取帳戶信息。 只是要問您要做什么,獲取您的帳號,找出要提取/存入的金額,然后在文本文件上進行更改。

我在FileNotFoundException方面遇到了一些麻煩。 我以前從未使用過它,並且不確定如何“捕獲”它。 我進行了一些搜索,找到了一些對我來說沒有意義的答案,例如“ try”語句和我不打算使用的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();
}

}

我不知道我是否理解正確,但是您是否意味着不知道如何使用try / catch構造?

最好的方法是閱讀教程:

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

快速添加您的代碼:

public static void deposit(String account, String amount) throws FileNotFoundException {
int i=-2;
try{
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");
} catch(FileNotFoundException e){
  //handling code
}
finally{
fileOut.close();
file.close();

}}

當涉及到異常時,有兩種方法可以處理它。 一種是像完成操作一樣引發異常(將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)

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM