是否可以使用相同的 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
。因此,必须捕获或声明该异常。因此,可以修改 towerOfHannoiMoves
以抛出 IOException
public static void towerOfHanoiMoves(int n,
String leftRod,
String rightRod,
String MidRod,
FileWriter outStream)
throws IOException
另一个注意事项:发布的代码缺少一些引号。例如,
outputStream.write("Input file not found.);
我正在从用户处接收一个输入文件和一个输出文件,并尝试从 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
。因此,必须捕获或声明该异常。因此,可以修改 towerOfHannoiMoves
以抛出 IOException
public static void towerOfHanoiMoves(int n,
String leftRod,
String rightRod,
String MidRod,
FileWriter outStream)
throws IOException
另一个注意事项:发布的代码缺少一些引号。例如,
outputStream.write("Input file not found.);