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
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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
Last edited by nasi; April 27th, 2010 at 02:45 AM.
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.
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
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....
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
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)
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
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:
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.
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
nasi (April 28th, 2010)
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.
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.