• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to write only a specified bytes into a ByteArrayOutputStream?

 
Tiya Khambadkone
Ranch Hand
Posts: 111
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a file that contains every line of length 40 bytes. I want only 25 bytes from every line of the file and rest 15 bytes should be ignored.
I have written the following code. It gives me a wrong result. Please suggest .



Actual Output :



Expected Output :

 
Tiya Khambadkone
Ranch Hand
Posts: 111
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got my answer .
I should have been using


and also added the following inside the while loop:

 
Tony Docherty
Saloon Keeper
Posts: 3144
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for posting your solution to your question.

Just remember, if you change your code to read in data from another type of stream eg FileInputStream then the read() method may not be not guaranteed to fill 'buffer' on each call and so you should really check the returned value you are storing in 'len' to make sure you have read in 41 bytes before outputting the data.
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiya Khambadkone wrote:I have a file that contains every line of length 40 bytes. I want only 25 bytes from every line of the file and rest 15 bytes should be ignored.

Just to point out: those aren't bytes, they're characters. And the fact that you refer to 'lines' suggests that you're reading a text file.

All of which means (I suspect) that you can use BufferedReader/BufferedWriter - and on version 8 maybe even Streams.

Winston
 
Campbell Ritchie
Marshal
Posts: 52602
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:. . . and on version 8 maybe even Streams.

Winston
Do you mean this sort of thing?OP: You should by now be familiar with creating a reader with try‑with‑resources.
The reader's lines() method returns a Stream<String> where each String represents one line. Since Streams run on lazy execution, calling limit(12L) stops the Stream after the 12th line has been read, if there are more than 12 lines in the first place..
You can collect things with the collect() method. That reduces the Stream to one value, which in this case will be a String.
The argument is (Collectors)#joining() which “Returns a Collector that concatenates the input elements into a String, in encounter order.” This is similar to using a StringJoiner.
In this instance, joining() causes the individual Strings in the Stream to be joined together, separated by the String ",%n" which is a comma followed by the “the platform-specific line separator as returned by System.getProperty("line.separator").” which you can find here with ctrl‑F‑"'n'".
 
Campbell Ritchie
Marshal
Posts: 52602
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to shorten the Strings in a Stream, try the map() method:-
....map(s -> s.substring(...))...
You cannot of course count the bytes since Strings are not made up of bytes in the first place; I suspect you need 25 chars instead.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!