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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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().g etBytes();
Hope this help you.
Bye, Giant.
convert the StringBuffer into a String and use the getBytes method.
byte[] bytearray=str.toString().g
Hope this help you.
Bye, Giant.
ASKER
The first exampel shows reading text, this is bytes
ASKER
byte[] bytearray=str.toString().g etBytes();
using this, does this not loose any information?
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) {
}
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) {
}
>>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?
As an append or do you want each one to be a different byte array?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
In this manner all the Experts participating at that question, could see your comment.
ASKER
Giant2
byte[] bytearray=str.toString().g etBytes();
using this, does this not loose any information.
As the string read consists of encyrpted binary data
byte[] bytearray=str.toString().g
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
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 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?
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.
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
OK, but in point of fact it won't work anyway, as AFAIK the 'whole load of bytes' may also contain \r\n
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
8-)
:)
(:
http://www.javaalmanac.com/egs/java.io/File2ByteArray.html?l=rel