Solved

How to read bytes using a DataInputStream

Posted on 2004-08-24
25
237 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:inzaghi
  • 10
  • 7
  • 5
  • +1
25 Comments
 
LVL 12

Accepted Solution

by:
Giant2 earned 200 total points
Comment Utility
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
0
 

Author Comment

by:inzaghi
Comment Utility
This reads the whole file into a byte array, I want to read line by line into a byte array
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
The first example read line by line.
You must only merge the two.
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
the process(str); method present in the first post could be used to convert the StringBuffer of the line into an Array of Byte.
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
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.
0
 

Author Comment

by:inzaghi
Comment Utility
The first exampel shows reading text, this is bytes
0
 

Author Comment

by:inzaghi
Comment Utility
byte[] bytearray=str.toString().getBytes();

using this, does this not loose any information?
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
   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) {
    }

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
Comment Utility
Before you answer that question inzaghi - stop! I've just noticed a potential problem in your last Q at

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21096902.html

depending on how the data are being stored and retrieved:


>>
userName#encyrptedData
userName#encryptedData

at the end of each line i need to enter a carriage return.
>>

What happens if the encrypted data also contain "\r\n"? Your delimiter would fail
0
 

Author Comment

by:inzaghi
Comment Utility
what do u recommend in that case?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 12

Expert Comment

by:Giant2
Comment Utility
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.
0
 

Author Comment

by:inzaghi
Comment Utility
Giant2

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

using this, does this not loose any information.
As the string read consists of encyrpted binary data
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
If the data in the encrypted bit are not too long, the second would be quite convenient, but will double storage space
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
>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.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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?
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
>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.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
0
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
Comment Utility
your better off using the approach I suggested in your previous question.
eg. something like:

dos.writeUTF(s);
dos.writeInt(encrypted.length);
dos.write(encypted);

and then when you read it:

String s = dis.readUTF();
int n = dis.readInt();
byte[] encrypted = new byte[n];
int remaining = n;
while (remaining>0)
{
   int read = dis.read(encrypted, n-remaining, remaining);
   remaining -= read;
}
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
:)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
(:
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now