我有一个发送 bmp 文件的服务器和一个 Android 客户端,我尝试在其中保存收到的数据。 我使用以下代码将数据保存在文件中:
...
byte[] Rbuffer = new byte[2000];
dis.read(Rbuffer);
try {
writeSDCard.writeToSDFile(Rbuffer);
} catch (Exception e) {
Log.e("TCP", "S: Error at file write", e);
} finally {
Log.e("Writer", "S: Is it written?");
}
...
void writeToSDFile(byte[] inputMsg){
// Find the root of the external storage.
// See http://developer.android.com/guide/topics/data/data- storage.html#filesExternal
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/download");
if (!(dir.exists())) {
dir.mkdirs();
}
Log.d("WriteSDCard", "Start writing");
File file = new File(dir, "myData.txt");
try {
// Start writing in the file without overwriting previous data ( true input)
Log.d("WriteSDCard", "Start writing 1");
FileOutputStream f = new FileOutputStream(file, true);
PrintWriter ps = new PrintWriter(f);
// PrintStream ps = new PrintStream(f);
ps.print(inputMsg);
ps.flush();
ps.close();
Log.d("WriteSDCard", "Start writing 2");
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i(TAG, "******* File not found. Did you" +
" add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
} catch (IOException e) {
e.printStackTrace();
}
}
但是在输出中我收到了对象 ID
例如 [B@23fgfgre[B@eft908eh ...
(其中 [ 表示数组。B 表示字节。@ 将类型与 ID 分开。十六进制数字是对象 ID 或哈希码。)
即使使用“PrintStream”而不是“PrintWriter”,我也会收到相同的结果...
如何保存真实输出?
最佳答案
尝试:
FileOutputStream f = new FileOutputStream(file, true);
f.write(inputMsg);
f.close();
关于java - 用java保存接收到的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39805280/