接收到的数据我有发送bmp文件和客户端在Android中,我尝试保存我接收到的数据服务器。保存在Java
...
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();
}
}
但在输出I接收对象ID
例如: 我使用以下代码保存在一个文件中的数据 [B @ 23fgfgre [B @ eft908eh ...
(其中[装置array.The乙装置byte.The @分离从ID.The十六进制数字的类型是对象ID或哈希码。)
我甚至接收使用,而不是“为PrintWriter”“为PrintStream”同样的结果...
如何节省实际输出?
您是否尝试过使用'write(byte [])'方法创建'FileOutputStream'中的'BufferedOutputStream'? –
'Rbuffer =新字节[2000];'。你的bmp文件是否小于2000字节? – greenapps
'dir.mkdirs();'。检查返回值。这可能是错误的。不要继续你的代码,如果它是假的,但显示敬酒和返回。 – greenapps