• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 56
  • Last Modified:

while writing a string in a text file using BufferedWriter, how to write in form of a paragraph in java

i am trying to write and store a string  in text file using buffered writer in java .i know how to write in a text file using buffered writer but  when i use it to write in the text file it writes in a single line.i want to write it in the text file  in different line in form of a paragraph.how to do that? please help
0
sangrag kanjilal
Asked:
sangrag kanjilal
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
krakatoaCommented:
You can use “\r” and “\n” to achieve carriage return and line feed, and then possibly arrange for some padding using “ “ to indent the beginning of the paragraph.
0
 
rrzCommented:
Please show us your code.  
What is the source of your text? Are you writing it at the command line?  Is it being copied from another file?  
Are you using Java 8 or 9? If yes, then why aren't you using  Files.newBufferedWriter  or Files.write ?
0
 
awking00Commented:
How is the string that you want to store generated?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
sangrag kanjilalAuthor Commented:
from a webpage using jsoup api
0
 
rrzCommented:
I haven't used jsoup. So, I can't help you.  
You should put jsoup in your question title. That way you will attract experts that use jsoup.  
Googling
jsoup preserve line breaks  
shows many ideas.
0
 
awking00Commented:
So you're not really "writing" a string, but reading a string from an html document? If so, what is actually being read from the document (e.g. all elements, certain elements, etc.)? Once you can determine which element(s) you need, it should be a rather simple exercise to generate a string value to be parsed or manipulated in the desired manner before writing it to the text file, although formatting as a paragraph can get a little complex. Something like the following might work -
String Filename =  "C:\\mydocs\mytextfile.txt";
FileWriter fw = new FileWriter(Filename);
BufferedWriter bw = new BufferedWriter(fw);
org.jsoup.nodes.Document doc = Jsoup.parse(htmlFile,"UTF-8");
Elements elements = doc.getElementsContainingText("The words or lines I want"); ==> example of getting certain elements
for (Element element: elements) {
  String txt = element.text();
  StringBuilder sb = new StringBuilder(txt); ==> should be helpful in manipulating string to paragraph format
  >>Do StringBuilder manipulations here<<
  String content = sb.toString();
  bw.write(content)
}

If that looks like it could work for you, I'll try and come up with the paragraph formatting issue. Good luck.
1
 
awking00Commented:
Could you post what you have so far? Perhaps you could also provide a link to an example webpage and what you would expect your test file to look like from that example.
0
 
krakatoaCommented:
Handled
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now