java - Java 文件读/写新手

标签 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();
}

}

最佳答案

我不知道我的理解是否正确,但是你的意思是你不知道如何使用 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();

} }

关于java - Java 文件读/写新手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32640133/

相关文章:

java.io.FileNotFoundException : File is not a normal file. 露天

java - 如何获取文件夹的位置?

spring-mvc - Spring MVC Multipart 文件上传随机 FileNotFoundException

java - 如何使用不相交的集合来实现迷宫?

java - 在运行时更改枚举 Java

java - java中如何获取打印机列表?

java.io.FileNotFoundException(未找到文件)使用扫描仪。我的代码有什么问题?

java - 如何将资源映射到枚举常量?

java - 在 Junit 中检查 2 个预期值

java - FileNotFoundException 从 SD 卡路径加载图像