如何将计算结果保存到java中的.txt文件中

时间:2013-12-05 23:45:51

标签: java

说我做了一个计算:

1000 * 2000

如何将计算结果打印到.txt文件?

3 个答案:

答案 0 :(得分:1)

您可以使用PrintWriter课程。类似的东西:

PrintWriter w = new PrintWriter("filename.txt", "UTF-8");
w.println(calculation);

这将创建一个具有指定名称的新文件,否则它将覆盖一个已存在的文件。

答案 1 :(得分:0)

我认为你的变量类型有问题。将Int转换为String。我没有尝试过代码。

Integer.toString(1000 * 2000);

答案 2 :(得分:0)

 public static void saveAs() throws IOException {
        System.out.println("Type In The New File Name"); //FILE NAME CHOICE
        Scanner fileChoice = new Scanner(System.in); //this scans userinput
        String newFile;                             //this is userinput
        newFile = fileChoice.nextLine();           //whatever they type is stored in this var
        File fileWriter = new File(newFile);       //opens a new file
        PrintWriter writeNew = new PrintWriter(new FileWriter(newFile, true));  //writes to it
        writeNew.println("TEXT HERE BLAH")         //writes the text to that file
        writeNew.close();                          //and dont forget to close

当userinput将要求您输入文件名时,指定它是.TXT,例如。输入hello.txt

problemsolved -