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.

Results 1 to 13 of 13

Thread: How to write to specific part of an html file using java

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default How to write to specific part of an html file using java

    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.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: How to write to specific part of an html file using java

    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.

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: How to write to specific part of an html file using java

    no, it is not the same always.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: How to write to specific part of an html file using java

    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.

  5. #5
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: How to write to specific part of an html file using java

    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.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to write to specific part of an html file using java

    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)

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: How to write to specific part of an html file using java

    Quote Originally Posted by nasi View Post
    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.
    It's pretty easy.. It's just a matter of reading the HTML file in, and writing it out again with the extra code.

    Please post the code you wish to add to the HTML file. I will write you a quick example.
    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.

  8. #8
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: How to write to specific part of an html file using java

    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

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: How to write to specific part of an html file using java

    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.

  10. The Following User Says Thank You to JavaPF For This Useful Post:

    nasi (April 28th, 2010)

  11. #10
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: How to write to specific part of an html file using java

    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.

  12. #11
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: How to write to specific part of an html file using java

    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.

  13. #12
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: How to write to specific part of an html file using java

    please be kind to me as before and answer my questions dear java specialist.

  14. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to write to specific part of an html file using java

    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.

Similar Threads

  1. Writing in a file using Java
    By JavaPF in forum File Input/Output Tutorials
    Replies: 4
    Last Post: December 17th, 2011, 04:33 PM
  2. how to transform an html file to a web site in java
    By nasi in forum Java Theory & Questions
    Replies: 9
    Last Post: March 28th, 2010, 11:06 PM
  3. why does this code read an html file uncontinuously
    By nasi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 25th, 2010, 09:09 PM
  4. write text to a file help
    By wolfgar in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 24th, 2009, 08:36 AM
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM

Tags for this Thread