public static void main(String[] args) {
Printable printable = new Printable() {
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
graphics.drawString(".", 1, 1);
return 0;
}
};
Book book = new Book();
PageFormat pf = new PageFormat();
pf.setOrientation(1);
Paper p = new Paper();
p.setSize(250.0D, 570.0D);
p.setImageableArea(0.0D, 0.0D, 250.0D, 570.0D);
pf.setPaper(p);
book.append(printable, pf);
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(book);
HashAttributeSet hs = new HashAttributeSet();
String printerName = "打印机的名称";
PrintService[] pss = PrintServiceLookup.lookupPrintServices((DocFlavor)null, hs);
hs.add(new PrinterName(printerName, (Locale) null));
pss = PrintServiceLookup.lookupPrintServices((DocFlavor) null, hs);
job.setPageable(book);
job.setPrintService(pss[0]);
job.print();
}
```java
public static void main(String[] args) {
Enumeration tempPortList;
CommPortIdentifier portIp;
tempPortList=CommPortIdentifier.getPortIdentifiers();
while(tempPortList.hasMoreElements()){
portIp=(CommPortIdentifier) tempPortList.nextElement();
portList.add(portIp);
int portType = portIp.getPortType();
if (portType == (CommPortIdentifier.PORT_SERIAL) && portIp.getName().equals("COM6")){
try {
serialPort = (SerialPort) portIp.open("COM6",2000);
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
outputStream = serialPort.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream,"GBK");
byte []cmd = new byte[3];
cmd[0] = 0x1B;
cmd[1] = 'J';
cmd[2] = 0x0D;
outputStream.write(cmd);
outputStream.write(1000);
writer.write("nihao");
writer.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}