FileNotFoundException 未被捕获或抛出
FileNotFoundException is not getting catched or thrown
它发生在两种不同的方法中,它都是 EDT(swing 组件)块的情况,并且不能做任何事情。
首先我尝试使用 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);
}
在第二种方法中,我使用 try catch,它也会向打印机发送一个字符串。
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,一切正常。不知道可以是什么。
尝试
Throws IOException
而不是找不到文件
在另一台电脑上测试过,现在工作正常。 Eclipse 或另一个中的 Java 出了点问题。
它发生在两种不同的方法中,它都是 EDT(swing 组件)块的情况,并且不能做任何事情。
首先我尝试使用 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);
}
在第二种方法中,我使用 try catch,它也会向打印机发送一个字符串。
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,一切正常。不知道可以是什么。
尝试
Throws IOException
而不是找不到文件
在另一台电脑上测试过,现在工作正常。 Eclipse 或另一个中的 Java 出了点问题。