• 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

BufferReader to Read String and write to file

 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

In my code I've huge HTML contents in string. HTML contents like,



I want to read this and write to a file. for that I did,


Its giving exception, Because I'm reading from stream. Please help me to get rid out of this.

Thanks:
Ramakrishna K.C
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramakrishna there is an error in you syntax. You are not supposed to pass you file or string in arguments to InputStreamReader instead a Stream reader
for example
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I gt that. But I dont want to pass file or something. I want read String itself. How can I read?
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check out FileUtils.writeStringToFile.
Pseudocode is like
 
Ranch Hand
Posts: 63
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without using any other library you can convert String to InputStream and then finally read with the help of BufferedReader.
 
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

Ramakrishna Udupa wrote:I gt that. But I dont want to pass file or something. I want read String itself. How can I read?


Erm ... so what do you want to do? Read or write?

Why don't you show us your code (just the relevant bits please), and exactly where you're having problems.

Winston
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand the point of reading the String with a BufferedReader. You already know how to write a String to a file, so why not just write your very big HTML String to the file directly?
 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you really really want to read a String, you can use a java.io.StringReader. The only reason I can think why anyone might actually need that is if they're using some library that expects a Reader rather than a String. Like perhaps if you wanted to use the readLine() method of BufferedReader (though there are other ways to do the same thing). That's not the case here, so it seems pointless. But you can do it if you want. The BufferedReader is also pointless in this case - the whole String content is already in memory; you're not gaining any speed by putting it in.
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks All,

I converted that into inputstream and then read from reader.

@Paul Clapham That sounds easy, But I'm not getting.
Ramakrishna K.C
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If all you want to do is write the HTML to a file, then why are you trying to read from the string with a BufferedReader? Just write the string to a file directly. No need to do complicated things with trying to read from the string.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic