简体   繁体   中英

Java long string of nulls

I read bytes[] from bluetooth inside a while loop to receive bytes then write them to an output file , but i see a long string of nulls added to each line why does that happen? how can i solve that?

    byte[] buffer = new byte[1024];
    String message;
    try {
    do {
    input.read(buffer);
    fil.write(buffer);
    fil.flush();
    }while(//some condition);
    }

this code is written inside a thread. output file: value +"long string of null"

update:the data written on the file is not formatted as i send them, how can i view them as they were send?

You're assuming that read(buffer); always reads 1024 bytes, when it will read up to 1024 bytes. It will return the amount of bytes read, so change your code to

int bytes = input.read(buffer);
fil.write(buffer, 0, bytes);

You can do somthing like this:

    int count;
    while ((count = in.read(buffer)) > 0) {
        fil.write(buffer , 0, count);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM