• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to write only a specified bytes into a ByteArrayOutputStream?

 
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 114
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got my answer .
I should have been using


and also added the following inside the while loop:

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic