是否可以使用相同的FileWriter文件并通过多种方法对其进行写入?

时间:2019-03-25 21:47:05

标签: java methods fileoutputstream printwriter

我正在从用户那里获取输入文件和输出文件,并试图从main方法和towerOfHanoiMoves方法写入输出文件。我试图将输出变量作为参数传递给towerOfHanoiMoves方法,但始终出现错误。

我收到的错误是:

 Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type IOException 
   at TowerofHanoiRecursive.towerOfHanoiMoves(TowerofHanoiRecursive.java:72)
   at TowerofHanoiRecursive.main(TowerofHanoiRecursive.java:41)

有没有一种方法可以使我从两个单独的方法写入同一输出文件?

我尝试使用PrintWriter,但这给了我同样的东西。 我也尝试过在towerOfHanoiMoves方法中刷新并关闭outStream,但这也不起作用。我还导入了所有必需的java.io文件,但是由于某些原因,它们没有显示在这里。

public class TowerofHanoiRecursive{


  public static void main(String[] args) throws IOException, EmptyFile,                                      
          FileNotFoundException{
    int n; //number of disks

    String rodLeft = "A", rodRight = "C", rodMiddle = "B";
            FileReader inputStream = null;
            FileWriter outputStream = null;
            BufferedReader str = null;  

   try {
        outputStream = new FileWriter(args[1]); // output file
        inputStream = new FileReader(args[0]);  // input file
            str = new BufferedReader(inputStream);  
            String nextLine; 
            File newFile = new File(args[0]);

     if (newFile.length()==0) { //Tests if input file is empty
       //if file is empty, the EmptyFile exception will be thrown
       throw new EmptyFile("Input file is empty");

       }

     while ((nextLine = str.readLine()) != null) {
        outputStream.write("----------------------------------------"
                + "------------------------\n");
        outputStream.write("Number of Disks in Starting Tower = " 
                + nextLine);
         n = Integer.parseInt(nextLine);

         towerOfHanoiMoves(n, rodLeft, rodRight, rodMiddle, 
              outputStream);

     }

   }catch (FileNotFoundException e) {
        outputStream.write("Input file not found.);
        outputStream.flush(); 
        if (outputStream != null) outputStream.close();

   }catch (EmptyFile e) {                               
        outputStream.write(e.getMessage());
        outputStream.flush();   
        if (inputStream != null) inputStream.close();
        if (outputStream != null) outputStream.close();
        str.close();
   } finally { 
        outputStream.write("");
        outputStream.write("\n\nSuccess!);
        outputStream.flush();

        if (inputStream != null) inputStream.close();
        if (outputStream != null) outputStream.close();
        str.close();
    }

}


  public static void towerOfHanoiMoves(int n, String leftRod, String 
            rightRod, String MidRod, FileWriter outStream) {

    if (n == 1) {
       outStream.write("Move disk 1 from ....");
    }
  }
}

1 个答案:

答案 0 :(得分:1)

特定的错误是write()方法会抛出IOException。结果,必须捕获或声明该Exception。因此,可以修改towerOfHannoiMoves以抛出IOException

    public static void towerOfHanoiMoves(int n,
        String leftRod,
        String rightRod,
        String MidRod,
        FileWriter outStream)
     throws IOException

另一个注意事项:那里发布的代码缺少一些引号。例如,

outputStream.write("Input file not found.);