Solved

How to read bytes using a DataInputStream

Posted on 2004-08-24
25
238 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse IDE - Cannot copy/paste from console output 8 132
Java array passed to SQL where clause 23 63
Error with Java/Cache JDBC Classpath 2 31
Apps blocked by Java 9 64
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

895 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

11 Experts available now in Live!

Get 1:1 Help Now