[英]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.