FileNotFoundException没有被捕获或抛出

时间:2017-03-25 20:28:49

标签: java printing path try-catch filenotfoundexception

它发生了两种不同的方法,它们都是EDT(摇摆组件)阻止并且无法做任何事情。

在第一次尝试使用 throws 时,它会将字符串发送到打印机,因此FileNotFoundException是因为找不到打印机(并且这不是问题) ,我知道打印机没有连接。)。

public void methodOne ()  throws PrintException, FileNotFoundException{
...
//a lot of lines
...
//not working code:
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
DocPrintJob pj = service.createPrintJob();
Doc doc=new SimpleDoc(bytes,flavor,null);
//and here is where the exception should be thrown:
pj.print(doc, null);

}

在第二种方法中,我使用尝试捕获,它也会将一个字符串发送到打印机。

public void methodTwo(){
String code2 = "1B700096FA";//hex
FileOutputStream os = null;
PrintStream ps=null;
try {
    os = new FileOutputStream("LPT1:POS-58");
    ps= new PrintStream(os);
    ps.print(toAscii(code2)); //--> here it freezes
    System.out.println("cashopen ");//--> not even arrives here
} catch (FileNotFoundException e) { //--> the exception is not being catched
    JOptionPane.showMessageDialog(this, "Can't open the cashdrawer");
    e.printStackTrace();
    }finally{
    ps.close();}
}
}

public StringBuilder toAscii( String hex ){
    StringBuilder output = new StringBuilder();
     for (int i = 0; i < hex.length(); i+=2) {
       String str = hex.substring(i, i+2);
       output.append((char)Integer.parseInt(str, 16));
     }
    return output;
}

其他例外情况正在发挥作用。欢迎任何想法。我正在使用Eclipse,Windows x64,Java se 8 121

修改

在另一台计算机Win7 x32,Java se 8 121 x32上测试过,一切都很好。不知道可能是什么。

2 个答案:

答案 0 :(得分:0)

尝试

Throws IOException

而不是filenotfound

答案 1 :(得分:0)

在另一台计算机上测试过,现在工作正常。 Eclipse或Java在另一个方面出了问题。

相关问题