Terminal User
Terminal User

Reputation: 883

Java How to read a file line by line. Each line is mixed with both ascII and binary

I have a file have multiple lines.

For each line the format is

"String A" "String B" "binary data"

What I want to do is adding "String C" in front of each line

"String C" "String A" "String B" "binary data"

Now I am using BufferedReader.readline(), it seems like this method has problem when reading binary data.

Can any one give me some suggestions on solving this question?

Upvotes: 1

Views: 423

Answers (3)

Voo
Voo

Reputation: 30235

A much more serious problem than "binary data may contain newlines" is obviously that binary data may not even contain valid unicode codepoints at all! Hence under no circumstances should you ever interpret binary data as text, but the other way is just fine.

Which means: Read the data into a bytebuffer and interpret it yourself.

Upvotes: 2

ayush
ayush

Reputation: 14578

I think You have to read binary buffers and interpret your format yourself, i.e. find the position of text extract bytes and transform them to String.

Readers can not read binary data. No way.

Upvotes: 2

Alex Calugarescu
Alex Calugarescu

Reputation: 848

If you have binary data in your file you shouldn't use the readLine() method which assumes that your line will end with '\r','\n' or "\r\n". The "binary data" can contain that sequence by chance and mess up your readLine().

Generally speaking, you shouldn't mix binary data with text data.

Upvotes: 3

Related Questions