我正在尝试在我的Android应用程序中开发打印选项,它将使用WIFI通过网络打印机从Android设备打印文本文件。为此,我使用带有IP地址和打印机端口号的套接字对象。 我的问题是,在我的文本文件中,文本对齐如下:
CV of Md. Akash
Name: Akash
Age: 30
但是在打印之后,输出将如下所示:
CV of Md. Akash
Name: Akash
Age: 30
N.B。我的代码: 1.数据收集代码并将其保存为SD卡中的文本
StringBuilder sb = new StringBuilder();
sb.append("CV of Md. Akash"+System.getProperty("line.separator"));
sb.append("Name: Akash"+System.getProperty("line.separator"));
sb.append("Age: 30");
String textBody = sb.toString();
//to save the textBody in SD card as txt file
File file = new File(Environment.getExternalStorageDirectory() +"/CvAkash.txt");
FileWriter fr = null;
try {
fr = new FileWriter(file);
fr.write(textBody );
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2。对于打印,我使用了此链接(Send files to WiFi printer)
中给出的以下代码 int port = 9100;
File file = new File(Environment.getExternalStorageDirectory()+"/CvAkash.txt");
try
{
client = new Socket("192.168.1.7", port);
byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file
outputStream = client.getOutputStream();
outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;