• Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferReader to Read String and write to file  RSS feed

 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • 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
 
Bhawana Gupta
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check out FileUtils.writeStringToFile.
Pseudocode is like
 
Niraj Jha
Ranch Hand
Posts: 63
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!