六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

java 打印机打印和开钱箱

[复制链接]

升级  96%

10

主题

10

主题

10

主题

童生

Rank: 1

积分
48
 楼主| 发表于 2013-1-15 02:45:36 | 显示全部楼层 |阅读模式
最近在网上查了一下,用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[i]);
                }
                fos.write("\n".getBytes());
                fos.flush();
            } catch (IOException ex) {
                Logger.getLogger(PrintTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表