leesonhomme 发表于 2013-1-15 02:45:36

java 打印机打印和开钱箱

最近在网上查了一下,用java实现了打印机打印以及打印机开钱箱功能。
代码如下:
      String os = System.getProperty("os.name");
      if(os.contains("Windows")){
            //Windows
            try {
                PrintWriter pw = new PrintWriter("lpt1");
                //普通打印
                pw.write("English-Windows"+"\n");
                pw.write("中文-Windows"+"\n");
                //打印机开钱箱指令
                char[] c = {27,'p',0,60,240};
                pw.write(c);
                pw.write("\n");
                pw.flush();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(PrintTest.class.getName()).log(Level.SEVERE, null, ex);
            }
      }else{
            //Linux
            try {
                FileOutputStream fos = new FileOutputStream("/dev/lp0");
                //普通打印
                fos.write("English-Linux\n".getBytes());
                fos.write("中文-Linux\n".getBytes("GBK"));
                //打印机开钱箱指令
                char[] c = {27,'p',0,60,240};
                for(int i=0;i<c.length;i++){
                  fos.write(c);
                }
                fos.write("\n".getBytes());
                fos.flush();
            } catch (IOException ex) {
                Logger.getLogger(PrintTest.class.getName()).log(Level.SEVERE, null, ex);
            }
      }
页: [1]
查看完整版本: java 打印机打印和开钱箱