将方法的结果写入.txt文件

时间:2016-02-25 21:31:08

标签: java file-io fileoutputstream

我的Java编程简介课程编程,我遇到了一些小问题。这是作业:

我要编写一个程序,接受来自用户的两个输入:一个是.txt文件的名称,第二个输入是long的整数。它将使用用户输入的文件名创建一个新的File对象,然后通过将File对象传递给PrintWriter构造函数来获取PrintWriter对象。如果找到该文件,它将读取整数。

然后,它将使用递归方法来反转用户输入的数字(例如,如果用户输入数字123456,则会将数字反转为654321)。然后它会将反转的数字写入.txt文件和屏幕。

这是我的代码:

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


public class Prog2c 
{
    public static void main(String[] args)
    {

    File inputFile = null;         // File object for user's file
    Scanner fileReader = null;     // reads user's file
    PrintWriter fileWriter = null; // writes to user's file
    Scanner scan = new Scanner(System.in); 
    String fileName = null; 

    long number = 0; 

    try
    {

        fileName = scan.nextLine();                                             
        number = scan.nextLong();   // reads the whole number from the user
        inputFile = new File(fileName); // creates new File object with user's file
        fileReader = new Scanner(inputFile);  // reads contents of user's file

        System.out.println("Program 2, Christopher Moussa, masc1754");          
        if (inputFile.exists())
        {
            System.out.println("The file " + fileName + " is ready for writing."); // throws FileNotFoundException if 
            fileWriter = new PrintWriter(inputFile); 
            number = reverseNumber(number);
            fileWriter.println(number); // performs recursive method on number inputted by user
            System.out.println("File " + inputFile + " exists? " + inputFile.exists()); // checks to see if file exists
            while (fileReader.hasNextLong())
            {
                number = fileReader.nextLong();
                System.out.println(number); // prints contents of the file
            }
        } 
    }

    catch (FileNotFoundException exception) // in case the file is not found or does not exist
    {
        System.out.println("FileNotFoundException file was not found: " + exception.getMessage());
        System.exit(0); 
    }

    finally 
    {
        if (null != fileReader)
        {
            scan.close(); // closes scanner
            fileWriter.close();
        }
    }

}

public static long reverseNumber(long number) // Recursive Method
{
    if (number < 10)                   // Base Case 1: In case the number
    {                                  // is a single digit number, I will
        System.out.println(number);    // just return the number
        return number;
    }
    else                               // Recursion: This will take the 
    {                                  // remainder of the original number
        System.out.print(number % 10); // and print it. It will then divide
        reverseNumber(number/10);      // the number by 10 to truncate it by
        return number;                 // one digit. It will then return the value.
    }
}
}

以下是我的问题:

当我运行程序时,写入.txt文件的编号是我输入的原始编号,而不是反转的编号(反转的编号应该写入.txt文件)。即使我有一个扫描仪应该读取文件的内容并将数字打印到屏幕上(这是在我的程序中声明,对吗?)。我的想法是为什么PrintWriter没有将反转的数字写入.txt文件是因为你不能将方法的结果分配给变量?我不完全确定。关于如何将反转的数字写入.txt文件然后将该.txt文件的内容(只有反转的数字)打印到屏幕上的任何建议/建议将非常感谢,谢谢。

1 个答案:

答案 0 :(得分:0)

您正在阅读文件,同时对其进行编辑。首先,完成将数字写入文件,然后接近作者。接下来打开读者。