Solved

java: read file and insert content

Posted on 2008-11-03
11
217 Views
Last Modified: 2013-11-23
I have a html file that I am reading in using filereader/bufferedreader.  I want to add content to the file when the first <body> tag is read.  What would the logic be for this condition and inserting data?
0
Comment
Question by:newbieal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22867528
Using filereader/bufferedreader you would have to create separate code using the writer versions of these classes (i.e. FileWriter) that will write the data to another file.  Since the buffered reader is giving each line one at a time, you would just insert your data through writer when you have read/written the opening body tag.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22867542
These may be helpful for you:

Java Tutorials -- Basic I/O:
http://java.sun.com/docs/books/tutorial/essential/io/

Java Practices -- Reading and Writing Files:
http://www.javapractices.com/topic/TopicAction.do?Id=42
0
 
LVL 4

Author Comment

by:newbieal
ID: 22867859
Thank you, but none of the examples demonstrate insertion of text at a particular point.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 4

Author Comment

by:newbieal
ID: 22867893
This is what I have so far. The text is appended at the end of the file and not after the <body> tag.
BufferedReader bIn = new BufferedReader(new FileReader(
			    "testing.html"));
		String line = bIn.readLine();
		while (line != null) {
			
		if(line.contains("<body>"))
		{
			createOrAppendFile("testing.html", "some text");
		}
		line = bIn.readLine();
		}
	}
	   private static void createOrAppendFile (String f, String text) throws IOException {  
		   BufferedWriter bw = new BufferedWriter (new FileWriter (f, true));  
		   bw.write (text);  
		   bw.newLine();  
		   bw.flush();  
		   bw.close();  
		   }  

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22868703
+Think you want to write to a different file.  You could always delete old and rename new one to old name at the end, but safer this way IMHO.
+Not sure you can insert data that way.  IIRC, the method you are using will write data at the end since you have the append option set to true.  Again, this may not be wrong if you are using a different file, since the text contained would only be up to point you have read and written from your other file so append would be right to insert data.

When you write to different file, you will see that you will need to write the original line data in first then do check for if you are at the <body> line and then insert additional data then when you code continues in loop it will write the remainder of the HTML thus leaving your new data inserted immediately after the <body> tag.
0
 
LVL 4

Author Comment

by:newbieal
ID: 22869512
Ok, I found this code that seems to do what I am looking for, however I need to insert content after the <body> tag.  How do I determine the line at which the content needs inserted? In this example the line number is provided via input.

http://www.java-tips.org/java-se-tips/java.io/insert-a-line-in-a-file.html
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 total points
ID: 22869796
That is basically the concept -- as you can see they are reading in the file and writing one line at a time to the new file then at the end they delete and rename.

In their case, a simple line number works.  For you though, you need logic like you have above to check if line contains "<body>" in its text.  To mimic what they are doing you have to change the logic around i++ to set the a temp string that has the contents of the line you have just written then the next time through it will check if temp string equals/contains string you are looking then write extra line(s) of data.

Then will continue to write lines of data until file is fully recreated.
0
 
LVL 4

Author Comment

by:newbieal
ID: 22871360
The <body> tag may contain some additional info, eg. <body lang=EN-US style='tab-interval:.5in'>

When I check for the tag what expression to I need to check it against so that my new values aren't inserted within the body tag.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22872854
You will probably need to look into writing a regular expression to check for "<body" + any amount of characters + ">".
0
 
LVL 4

Author Comment

by:newbieal
ID: 22872886
Yeah, that's what I thought.  Do you have an example of a regex for my specific case?
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22872940
You can try "<body[a-zA-Z0-9]*>" but I would give it a try it and then create a question in the regular expressions zone...

Hopefully that helps!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
swing controls 2 34
Fast way to search item into Java Array (Rhino compatible) 2 45
program arguments vs VM arguments 4 54
junit example issue 2 50
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Suggested Courses

736 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question