Link to home
Start Free TrialLog in
Avatar of inzaghi
inzaghi

asked on

How to read bytes using a DataInputStream

I have a file which I have written a whole load of bytes followed by a new line.
out.writeByte('\r');
out.writeByte('\n');

I want to read line by line and them add them to a byte array, the length of each line varies.

How can I read line by line into a byte arrray?
ASKER CERTIFIED SOLUTION
Avatar of Giant2
Giant2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Giant2
Giant2

Avatar of inzaghi

ASKER

This reads the whole file into a byte array, I want to read line by line into a byte array
The first example read line by line.
You must only merge the two.
the process(str); method present in the first post could be used to convert the StringBuffer of the line into an Array of Byte.
Something like this:
convert the StringBuffer into a String and use the getBytes method.

byte[] bytearray=str.toString().getBytes();

Hope this help you.

Bye, Giant.
Avatar of inzaghi

ASKER

The first exampel shows reading text, this is bytes
Avatar of inzaghi

ASKER

byte[] bytearray=str.toString().getBytes();

using this, does this not loose any information?
   try {
        BufferedReader in = new BufferedReader(new FileReader("infilename"));
        String str;
        while ((str = in.readLine()) != null) {
            byte[] bytes=str.getBytes();
            //now in bytes there is your line of byte
        }
        in.close();
    } catch (IOException e) {
    }

Avatar of CEHJ
>>I want to read line by line and them add them to a byte array, the length of each line varies.

As an append or do you want each one to be a different byte array?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of inzaghi

ASKER

what do u recommend in that case?
Sorry CEHJ, but I think your last post must be appended to the question you are referring and not here.
In this manner all the Experts participating at that question, could see your comment.
Avatar of inzaghi

ASKER

Giant2

byte[] bytearray=str.toString().getBytes();

using this, does this not loose any information.
As the string read consists of encyrpted binary data
>>last post must be appended to the question you are referring and not here.

I can do that as well, but this matter will probably change the course of this question now

>>what do u recommend in that case?

Two possibilities:

1. Include the length of each atom in the data (you don't need delimiters then)
2. Store the encrypted data as ascii hex string, e.g.

08ffa322e3

etc. then there won't be any danger of control characters in there
If the data in the encrypted bit are not too long, the second would be quite convenient, but will double storage space
>I can do that as well
little touchy.
I believe if there are problem with the question "A", it could be reopened because user asking a correct solution see the solution offered for question "A" and not an intervention of question "X".
But, as normal [], it's a decision of people who wrote.
>>little touchy.

I or you? If i, not remotely.

You also seem to be forgetting that the purpose here is to provide questioners with answers to their problem. If, one way or another, the question has not been defined correctly due to a flawed premise, do you think it's legitimate to get points for answering the 'wrong' question?
>do you think it's legitimate to get points for answering the 'wrong' question?

No. Infact he could be delete/close the question. And even reopen other.
But, as normal [], it's a decision of people who wrote.

The question I answer was:
>I have a file which I have written a whole load of bytes followed by a new line.
>out.writeByte('\r');
>out.writeByte('\n');
>I want to read line by line and them add them to a byte array, the length of each line varies.
>How can I read line by line into a byte arrray?

and not other.

Sincerely, Giant.
>>The question I answer was:

OK, but in point of fact it won't work anyway, as AFAIK the 'whole load of bytes' may also contain \r\n
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
8-)
:)