0

I have server that sends bmp file and a client in Android where I try to save the data that i received. I save the data in a file using the following code:

...
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();
    }
}

But in the output I receive the object ids

e.g. [B@23fgfgre[B@eft908eh ...

(Where [ means array.The B means byte.The @ separates the type from the ID.The hex digits are an object ID or hashcode.)

The same result i receive even using "PrintStream" instead of "PrintWriter"...

How can I save the real output?

G.V.
  • 709
  • 2
  • 11
  • 17
  • 1
    Have you tried creating a `BufferedOutputStream` out of your `FileOutputStream` and using the `write( byte[] )` method? – Mike Nakis Oct 01 '16 at 10:06
  • 1
    `Rbuffer = new byte[2000];`. Is your bmp file smaller than 2000 bytes? – greenapps Oct 01 '16 at 11:33
  • `dir.mkdirs();`. Check the return value. It can be false. Dont continue with your code if it is false but display a toast and return. – greenapps Oct 01 '16 at 11:35
  • Instead of providing an url which is not clickable you just should have answered my question. – greenapps Oct 01 '16 at 12:52
  • @greenapps I don't know how big will be the received file. But the code has an issue even with the write (byte[]) method as it creates too big files... – G.V. Oct 01 '16 at 12:57
  • Your code cannot receive files bigger than 2000 bytes. Thats why i asked. You should have understood that. – greenapps Oct 01 '16 at 12:59
  • @greenapps Yes I know that. Changing the length of the array and using write() method, I receive an extremely big file e.g 100MB ..... So I think there is another I in the code... – G.V. Oct 01 '16 at 13:44
  • The size of the array was ok. 2000 bytes could do the job. The code is wrong. Adapt your code too read and write in a loop. – greenapps Oct 01 '16 at 13:54

2 Answers2

2

Try:

FileOutputStream f = new FileOutputStream(file, true);
f.write(inputMsg);
f.close();
Kamil
  • 1,456
  • 4
  • 32
  • 50
1

The word "print" in the names of PrintWriter and PrintStream should give you a hint that they generate text. If you read the documentation thoroughly, that's explicitly stated in there.

https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#print(java.lang.Object)

Specfically, the documentation for the print(Object obj) overload of PrintWriter that you are using explicitly says

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.

Clearly, that's not what you want. You have an array of bytes and you want to write those bytes into a file, exactly as they are. So, forget about PrintWriter and PrintStream. Instead, do something like this:

BufferedOutputStream bos = new BufferedOutputStream(f);
bos.write(inputMsg);
//bos.flush(); stop. always. flushing. close. does. that.
bos.close();
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • I used it but the file gets too big in a couple of seconds... According to java manual write() : Writes b.length bytes from the specified byte array to this file output stream. – G.V. Oct 01 '16 at 12:04
  • That's an entirely different question. We have no idea how you use this code, where you get your data from, at what rate the data comes in, etc. Post a new question, complete with an http://stackoverflow.com/help/mcve AND sample input data, and see if you get an answer to that question. – Mike Nakis Oct 01 '16 at 13:48
  • I do not know what `dis.read(Rbuffer);` is, or what it does, but one thing I can see for sure is that you are given no indication about how many bytes you are reading. So, your code certainly has other problems, too. – Mike Nakis Oct 01 '16 at 13:49