所以我正在编写一个接收 2 个字符串的代码。字符串是“input#.txt”或“output#.txt”,# 符号将替换为使用的任何数字文件。现在输入文件中是我需要获取的信息。如何确定该 input.txt 文件是否可以打开以及如何打开它。
我尝试过缓冲阅读器并尝试将字符串作为文件。
import java.util.*;
import java.io.*;
public class Robot {
public static void readInstructions(String inputFileName, String outputFileName) throws InvalidRobotInstructionException{
try{
BufferedReader input = new BufferedReader(new FileReader(inputFileName));
File inner = new File(inputFileName);
Scanner in = new Scanner(input);
PrintWriter wrt;
wrt = new PrintWriter(outputFileName);
if(input.readLine() == null){
System.out.println("Input file not found.");
return;
}
最佳答案
这将读取以下位置的文件:
Scanner input = new Scanner(new File("input5.txt"));
不要忘记在主方法中添加抛出 FileNotFoundException
编辑:我看到您添加了代码。
关于java - 读取输入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36241613/