Solved

How to read bytes using a DataInputStream

Posted on 2004-08-24
25
241 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
ID: 11881746
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11881753
0
 

Author Comment

by:inzaghi
ID: 11881825
This reads the whole file into a byte array, I want to read line by line into a byte array
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

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

Expert Comment

by:Giant2
ID: 11881885
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
ID: 11881982
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
ID: 11882032
The first exampel shows reading text, this is bytes
0
 

Author Comment

by:inzaghi
ID: 11882052
byte[] bytearray=str.toString().getBytes();

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

Expert Comment

by:Giant2
ID: 11882075
   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
ID: 11882100
>>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
ID: 11882421
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
ID: 11882504
what do u recommend in that case?
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11882526
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
ID: 11882599
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
ID: 11882662
>>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
ID: 11882718
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
ID: 11882807
>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
ID: 11882886
>>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
ID: 11882952
>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
ID: 11883006
>>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
ID: 11887128
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
ID: 12198659
8-)
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12200766
:)
0
 
LVL 92

Expert Comment

by:objects
ID: 12204546
(:
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert from xls to xlsx using java 7 96
printf performancy 11 86
Java class and jar 3 43
Java Inheritance super keyword use 8 40
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

730 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