[英]how to transfer the records in rms(j2me) to j2se through bluetooth
现在,这里是用于发送字符串的j2me mobile的编码:
String s="hai";
try{
String url = "btspp://001F81000250:1;authenticate=false;encrypt=false;master=false";
StreamConnection stream = null;
InputStream in;
OutputStream out;
stream = (StreamConnection) Connector.open(url);
out=stream.openOutputStream();
String s=tf.getString();
byte size=(byte) s.length();
out.write(size);
out.write(s.getBytes());
out.flush();
out.close();
stream.close();
}
catch(Exception e){
}
现在,j2se的编码用于接收String:
StreamConnectionNotifier notifier=null;
try{
String url = "btspp://localhost:"+new UUID("1101", true).toString()+";name=PCServerCOMM;authenticate=false";
System.out.println(LocalDevice.getLocalDevice().getBluetoothAddress()+"\nCreate server by uri: " + url);
notifier= (StreamConnectionNotifier) Connector.open(url);
while(true){
System.out.println("waiting....");
StreamConnection con = notifier.acceptAndOpen();
System.out.println("Got connection..");
InputStream is=con.openInputStream();
//byte b[]=new byte[40];
/*
while(is.available()>0){
System.out.print((char)is.read());
}*/
//is.read(b, 0, 40);
int size=is.read();
byte b[]=new byte[size];
is.read(b, 0, size);
File f=new File("d://test.xml");
FileOutputStream fo=new FileOutputStream(f);
fo.write(b,0,b.length);
fo.close();
con.close();
System.out.println(new String (b));
}
//printing(f);
} catch(Exception e){
JOptionPane.showConfirmDialog(new JFrame(), e.getMessage());
}
我尝试使用这种编码进行数据传输,但是它不是成功的编码,因为当我们发送的字符串太长时,接收端就会出现问题。 我该如何解决?
还有什么其他方法可以将rms中的数据传输到j2se,如果可以,请帮助我....请快速回复...
您在此处读写的方式只能正确写出最多255个字符的字符串,而且这些字符串在默认编码中仅占用相同数量的字节。
在写作方面:
byte size=(byte) s.length();
将字符串的长度转换为一个字节,因此仅占用长度的低8位。 因此,最多只能写入255个正确的长度。 s.getBytes()
将String转换为字节数组-该数组可能比原始字符串(以字符为单位)更长(以字节为单位)。 此转换使用发送设备的默认编码。 在阅读方面:
int size=is.read();
读取之前写入的长度,然后创建一个字节数组。 is.read(b, 0, size);
将一些字节读取到此数组中-它不一定会填充整个数组。 因此,我们有:
如何解决这个问题:
readUTF
和writeUTF
方法。 它们解决了您的所有问题(如果您的字符串在此处使用的经过修改的UTF-8编码中最多占用65535个字节)。 getBytes()
和new String(...)
,请使用具有显式编码名称并为其提供相同编码的变体(我建议使用"UTF-8"
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.