java - 用java保存接收到的数据

标签 java android printwriter objectid printstream

我有一个发送 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/

相关文章:

java - Java 中的 getConstructor 抛出 NoSuchMethodException

java - Postman 多部分/表单数据错误 : Missing start boundary

java - 崩溃保存按钮切换状态

android - Bootstrap 2.3.2 - 不要将移动布局用于平板电脑纵向

android - Cookie 在 Android 的 WebView 中无法正常工作

java - 使用 lambdaj 从嵌套数组中提取对象

android - 一次打开一个切换按钮

java - getRequestDispatcher()、getOutputStream() 和 getWriter() 有什么关系吗?

Java 服务器-客户端 |服务器不会收到第二个请求

java - 为什么文件被写入空白?