Java 未捕获或抛出FileNotFoundException
它是通过两种不同的方法发生的,这两种方法都是EDT(swing组件)阻塞,并且什么都做不了 在第一次尝试中,我使用抛出,它向打印机发送字符串,因此FileNotFoundException是因为找不到打印机(这不是问题,我知道打印机未连接) 在我使用的第二种方法中,try-catch也会向打印机发送一个字符串Java 未捕获或抛出FileNotFoundException,java,printing,path,try-catch,filenotfoundexception,Java,Printing,Path,Try Catch,Filenotfoundexception,它是通过两种不同的方法发生的,这两种方法都是EDT(swing组件)阻塞,并且什么都做不了 在第一次尝试中,我使用抛出,它向打印机发送字符串,因此FileNotFoundException是因为找不到打印机(这不是问题,我知道打印机未连接) 在我使用的第二种方法中,try-catch也会向打印机发送一个字符串 public void methodTwo(){ String code2 = "1B700096FA";//hex FileOutputStream os = null; PrintSt
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;
}
public-void-methodTwo(){
字符串代码2=“1B700096FA”;//十六进制
FileOutputStream os=null;
PrintStream ps=null;
试一试{
os=新文件输出流(“LPT1:POS-58”);
ps=新打印流(os);
ps.print(toAscii(代码2));//-->在这里它冻结了
System.out.println(“cashopen”);//-->甚至没有到达这里
}catch(FileNotFoundException e){/-->未捕获该异常
showMessageDialog(这个“无法打开现金抽屉”);
e、 printStackTrace();
}最后{
ps.close();}
}
}
公共StringBuilder toAscii(字符串十六进制){
StringBuilder输出=新的StringBuilder();
对于(int i=0;i
其他例外情况也在起作用。任何想法都欢迎。我正在使用Eclipse、Windowsx64、JavaSE8121
编辑
在另一台计算机win7x32、javase8121x32上测试,一切正常。不知道会是什么。试试看
Throws IOException
而不是在另一台计算机上测试的filenotfound,现在工作正常。另一个版本中的Eclipse或Java出现问题。不工作,没有抛出任何错误或异常。EDT只是冻结.println str,看看它在做什么。在这两种情况下,在到达.print()方法之前,一切都很好。
Throws IOException