将收到的数据保存在 java
Save received data in java
我有发送 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 ...
(其中 [ 表示 array.The B 表示 byte.The @ 将类型与 ID.The 十六进制数字分开是对象 ID 或哈希码。)
即使使用 "PrintStream" 而不是 "PrintWriter" 我也会收到相同的结果...
如何保存真实输出?
尝试:
FileOutputStream f = new FileOutputStream(file, true);
f.write(inputMsg);
f.close();
PrintWriter
和 PrintStream
的名字中的单词 "print" 应该给你一个提示,它们生成文本。如果您仔细阅读了文档,那里面有明确说明。
https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#print(java.lang.Object)
具体来说,您正在使用的 PrintWriter
的 print(Object obj)
重载的文档明确指出
Prints an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
显然,这不是您想要的。你有一个字节数组,你想把这些字节原封不动地写入一个文件。所以,忘记 PrintWriter
和 PrintStream
。相反,做这样的事情:
BufferedOutputStream bos = new BufferedOutputStream(f);
bos.write(inputMsg);
//bos.flush(); stop. always. flushing. close. does. that.
bos.close();
我有发送 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 ...
(其中 [ 表示 array.The B 表示 byte.The @ 将类型与 ID.The 十六进制数字分开是对象 ID 或哈希码。)
即使使用 "PrintStream" 而不是 "PrintWriter" 我也会收到相同的结果...
如何保存真实输出?
尝试:
FileOutputStream f = new FileOutputStream(file, true);
f.write(inputMsg);
f.close();
PrintWriter
和 PrintStream
的名字中的单词 "print" 应该给你一个提示,它们生成文本。如果您仔细阅读了文档,那里面有明确说明。
https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#print(java.lang.Object)
具体来说,您正在使用的 PrintWriter
的 print(Object obj)
重载的文档明确指出
Prints an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
显然,这不是您想要的。你有一个字节数组,你想把这些字节原封不动地写入一个文件。所以,忘记 PrintWriter
和 PrintStream
。相反,做这样的事情:
BufferedOutputStream bos = new BufferedOutputStream(f);
bos.write(inputMsg);
//bos.flush(); stop. always. flushing. close. does. that.
bos.close();