是否可以使用相同的FileWriter文件并从多个方法写入它?

问题描述 投票:0回答:1

我正在从用户接收输入文件和输出文件,并尝试从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 ....");
    }
  }
}
java methods fileoutputstream printwriter
1个回答
1
投票

特别的错误是write()方法可以抛出IOException。因此,必须捕获或声明该异常。所以,人们可以修改towerOfHannoiMoves扔掉IOException

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

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

outputStream.write("Input file not found.);
© www.soinside.com 2019 - 2024. All rights reserved.