我希望用户在命令行上输入文件名。如果他们不输入任何内容,我应该打印信息说文件未处理并退出。这是我的 try catch block
try{
parser.openFile(args[0]);
if(parser.getCounter() == 3)
{
System.out.println("File was processed: true");
}
else
{
System.out.println("File was processed: false. Missing information.");
}
//found = true;
}
catch (IOException e)
{
e.printStackTrace();
System.out.println("File was processed: false. Re-enter filename.");
//fName = keyboard.nextLine();
}
openFile 方法在我的类中,就在这里,以防有人需要它:
public void openFile (String filename)throws IOException{
fis = new FileInputStream(filename);
br = new BufferedReader(new InputStreamReader(fis));
String thisLine;
thisLine = br.readLine();
while(thisLine != null)
{
lines.add(thisLine);
thisLine = br.readLine();
}
}
不知何故“文件已处理:错误。重新输入文件名。”当命令行中没有文件名时不打印。有人可以帮我吗?
最佳答案
您可能会遇到不同的异常。也许
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
尝试验证args的内容
示例:
public class TesteArgs {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Inform a valid name...");
System.exit(0);
}
// continue handling your args param
System.out.println(args[0]);
}
}
关于java try catch block 不打印我想要的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33663515/