I have an html file and I want to add some lines to specific part of that file using java. would you please guide me to the solution
Printable View
I have an html file and I want to add some lines to specific part of that file using java. would you please guide me to the solution
Hello nasi,
Does the specific part of the file always stay the same?
Please post an example of the HTML file and show me where you would like to insert the code.
no, it is not the same always.
Well you will need to identify where abouts in the HTML you wish to insert the extra code.
How else will the program know where to look?
If you want to add the code at the start or the end of the file, that won't be a problem.
Please explain where abouts you would be inserting the extra code....
actually it is not a predefined position, and it varies for different html files, the only thing that I can say is that it is inside the codes of my html file. if I can't embed my codes inside the html file, it would be good to add them to the end of the file but I don't know how to do it as well.
have you looked into using java script/jsp? these can easily and fairly quickly provide dynamic content to a webpage (PHP and ASP are some other good choices, too)
when I do that it overwrites my file and replaces the original html code with new one. I will try to send u what I am doing,I have to simplify it. it is now multiple classes
Yes please attach your code. You can stop the over write by doing this:
Code :import java.io.*; public class WriteFile { public static void main(String[] args) throws IOException { Writer output = null; File file = new File("myFile.txt"); output = new BufferedWriter(new FileWriter(file, [B]true[/B])); output.write("Whatever string you want here"); output.close(); System.out.println("File written"); } }
Without the true, it overwrites the file each time.
You should be reading in the original file and saving it out as something new. Then you could add code to delete the original file and rename the new one.
Now if I want to add text inside the body of my html file, how should I do that? I want to select a word or phrase in the file, and add some links to it. Actually I want to make a hypertext file from my html file. so I need add some thing like this any where a selected text exists.
Code :The <a href="http://java.sun.com/javase/6/docs/api/">Students</a> should learn</p>
here Students is the selected text.
I think you have chosen a wrong place to publish your great Ideas. if you don't know I 'll tell you that here is for Java programming discussions, not dating.
please be kind to me as before and answer my questions dear java specialist.
If you want to insert text, the easiest way is to actually over-write the whole file.
First, identify where it is you want to insert the html code. Then, write out everything you read from the file up to that point. Then, write out the stuff you want to insert. Finally, write out everything that's after the insertion point.