尝试使用java保存文件时出错

时间:2014-12-04 19:45:24

标签: java save calculator filenotfoundexception printwriter

我正在制作一个可以将内存保存到文件中的内存计算器,但我一直在找"找不到文件"请帮忙! 这是从我的记忆计算器扩展的保存记忆计算器:

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



class SaveSciMemCalc extends SciMemCalc {

public int displayMenu(){

    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);
    int choice = -1;
    while (choice < 1|| choice > 8){
        System.out.println();
        System.out.println("Menu:");
        System.out.println("1. Add");
        System.out.println("2. Subtract");
        System.out.println("3. Multiply");
        System.out.println("4. Divide");
        System.out.println("5. Power");
        System.out.println("6. Logarithm");
        System.out.println("7. Clear");
        System.out.println("8. Save");
        System.out.println("9. Quit");
        System.out.println();
        System.out.println("What would you like to do?");
    choice = input.nextInt();
    if (choice < 1|| choice > 9){
        System.out.println(choice +" wasn't one of the options");
    }
    if (choice == 9){
        System.out.println("Thank you, good bye!");
        System.exit(0);
    }
    }
    return choice;
}
String save(String saveList) throws FileNotFoundException{
    setCurrentValue(0);
    File dir = new File(saveList);
    PrintWriter pw = new PrintWriter(dir);
    pw.println(dir);
    pw.close();
    return "Saving...";
}

}

这是我运行计算器的驱动程序类:

import java.io.FileNotFoundException;
import java.util.Arrays;




public class SaveSciMemCalcDriver {

public static void main(String[] args) {
    String dog = "";
    SaveSciMemCalc calc = new SaveSciMemCalc();
    int p = 0;
    String[] saves = new String[10];
    while (true){

        System.out.println("The current value is " + calc.getCurrentValue());

        double currentValue = calc.getCurrentValue();

        int choice = calc.displayMenu();

        double second = 0;
        String o = null;
        if (choice < 6) {
            second = calc.getOperand("What is the second number? ");
        }

        if (choice == 1) {
            calc.add(second);
            o = " + ";
        } else if (choice == 2){
            calc.subtract(second);
            o = " - ";
        } else if (choice == 3){
            calc.multiply(second);
            o = " * ";
        } else if (choice == 4){
            calc.divide(second);
            o = " / ";
        } else if (choice == 5){
            calc.power(second);
            o = " ^ ";
        } else if (choice == 6){
            calc.log();
            o = "log";
        } else if (choice == 7){
            calc.clear();
            Arrays.fill(saves, null);
        } else if (choice == 8){
            for (int i=0;i<saves.length;i++){
                dog=dog+saves[i]+"\n";
            }
            try {
                calc.save(dog);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            Arrays.fill(saves, null);
        }
        if (choice == 6){
            saves[p] = o+currentValue+" = "+calc.getCurrentValue();
        }else{
            saves[p] = currentValue+o+second+" = "+calc.getCurrentValue();
        }
    p++;
    if (choice == 7){
        p=0;
    if(choice == 8){
        p=0;
    }
    }
}
}
}

2 个答案:

答案 0 :(得分:1)

看起来你正在积累要保存在dog中的,然后将dog作为saveList参数传递,然后将其用作文件名。

for (int i=0;i<saves.length;i++) {
    dog=dog+saves[i]+"\n";          // collect what to save
}

...

calc.save(dog);                     // pass it to save

String save(String saveList) throws FileNotFoundException{
                   ^^^^^^^^ = contains *what* to save

    setCurrentValue(0);
    File dir = new File(saveList);   <-- Passed as filename to File constructor
    PrintWriter pw = new PrintWriter(dir);
    pw.println(dir);                 <-- ...as well as written to file
    pw.close();
    return "Saving...";
}

相反,做

String save(String filename, String saveList) throws FileNotFoundException{
            ^^^^^^^^^^^^^^^

    setCurrentValue(0);
    File dir = new File(filename);
                        ^^^^^^^^
    PrintWriter pw = new PrintWriter(dir);
    pw.println(saveList);
               ^^^^^^^^
    pw.close();
    return "Saving...";
}

答案 1 :(得分:0)

文件构造函数缺少文件的路径。 空字符串File构造函数引发FileNotFoundException