繁体   English   中英

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

[英]Is is possible use the same FileWriter file and write to it from multiple methods?

我正在从用户那里获取输入文件和输出文件,并尝试从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 ....");
    }
  }
}

特殊的错误是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.);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM