Results 1 to 9 of 9

Thread: Programming question

  1. #1

    Post Programming question

    Hello Folks,

    A quick question for those brainy guys.

    How can we generate output in the HTML format. Say for example, we write a program which reads from a text file with certain information (data/variable). We prompt the user for certain information which will be substituted for the variable. The resulting file is required to be generated in the HTML format. Say, we want to use Java to write the program.

    I hope am making sense. If not, please let me know !

    P.S: This is not any assignment that I have been asked to do. I saw this question in "problem sets" of one of the popular university.
    Things that don\'t kill you make you stronger !

  2. #2
    I don't know Java, but in peseudo code it would be something like;

    Dim strPersonName as string // This is your variable you have collected
    open htmlpage.html for write as #1 // Open a file for writing to. Note it has a .html suffix.
    Write #1 "<title> This is a page on " & strPersonName & "</title>" // Write the html (with the speech marks, this means it's literal rather than a variable), and put the & sign to join the variable and html code together (to one line).

    etc etc.

    This is the basic method. All you have to do from then on is repeat/add more variables/code in.
    \"Death is more universal than life; everyone dies but not everyone lives.\"
    A. Sachs

  3. #3
    Banned
    Join Date
    Aug 2001
    Location
    Yes
    Posts
    4,424
    *Moved from Tutorials*

  4. #4
    Thanks da'dodo ! Looks good to me. Atleast I get idea, how I should go about it. I don't have much of experience with File I\O but, I think I can do it with some reference books besides me.
    Things that don\'t kill you make you stronger !

  5. #5
    Junior Member
    Join Date
    Apr 2003
    Posts
    2
    you're going to want to use the java.io package
    import java.io.*;
    probably using classes such as Buffered or File Reader / Writer.
    The documentation is at
    http://java.sun.com/j2se/1.4.2/docs/api/index.html

    -
    nickel

  6. #6
    Thanks Nickel ! Yes, I do believe we will be needing that package to read/write into the files in order to create html file which then obviously can be invoked using a browser. I think the essence of the problem lies in writing an HTML file which will accomodate every need "dynamically", like to create links etc.
    Things that don\'t kill you make you stronger !

  7. #7
    Okay why don't we try a little bit of Java code that may be able to answer you question by example. First off, you will need to be familiar with the general structure of a Java program. If you don't know that then you should go find an introductory Java tutorial, there are probably thousands out there. I'll write some code out and then try to explain it.

    import java.io.*;
    import java.util.ArrayList;

    public class HTMLFileCreator {

    public static void main(String[] args) {

    try {
    // Lets declare some variables

    File input, output; /* Our input/output files */
    PrintWriter writer; /* PrintWriter is a class that writes text */
    BufferedReader fileReader, inputReader; /* Makes managing input easier */
    String text, input; /* Holds our string Data */
    ArrayList list /* Holds multiple lines of text */
    // Set variables

    input = new File("inputFile.txt");
    output = new File("output.html");
    inputReader = new BufferedReader(new InputStreamReader(System.in));
    fileReader = new BufferedReader(new FileReader(input));
    writer = new PrintWriter(new FileWriter(output), true);
    list = new ArrayList();

    // Reading user input

    input = inputReader.readLine();
    text = "<html><title>Test Page</title><body><h1>" + input + "</h1></body></html>"
    writer.println(text);


    // Reading from a file

    while((input = fileReader.readLine()) != null)
    list.add(input);

    // Writing data to a file

    text = "<html><title>Test Page</title><body>"
    writer.println(text);
    for(int i = 0; i < list.size();i++)
    writer.println((String)list.get(i));
    text = "</body></html>"
    writer.println(text);

    }
    catch(IOException ioe) {System.err.println("There was an I/O error");}
    }
    }

    Ok, this code is meant to be more of a guide and may not even compile (I'm lazy) but it should point out some of the basic I/O operations that I think you're interested in.

    First off, as nickle stated above, you're going to need to import java's I/O package so at the top of your program you have the "import java.io.*;". Also if you want to use ArrayLists you have to import that class as well.

    After declaring the class and main methods we start to create some variables. There are a few different constructors for File objects in Java, but I think the easiest one is the one that we use here. You can see that it just takes the File's name as it's only parameter. If the file does not currently exist, as is probably the case for our output file, then it is created for us.

    Don't let the syntax for the BufferedReader objects scare you. You'll get used to it after you use them for a little while. The reason that one takes an InputStreamReader, and another takes a FileReader as it's parameter is that in one instance we're reading from user input (System.in) and in another we're using a file (input). The BufferedReader supplies us with the readLine() function that can be used for both the file and the keyboard.

    The PrintWriter object is a pretty simple way of writing lines of text (html in this case) to a file. You might have noticed that it takes two parameters, a FileWriter object, and a boolean value. This boolean value tell the PrintWriter that you want to flush the data automatically, and I added it so that you don't have to worry about doing that yourself.

    So if you take a look, you'll see that in one instance, we read a line of user input, stick it in the middle of some HTML tags, and then write the whole String to our output file. In the other instance, we read the lines of our input file and store them in an ArrayList object. We write some HTML to output, then cycle through the ArrayList writing the contents of the input file, and lastly we write some closing HTML to the output file. If you're not familiar with the ArrayList object I suggest you check out the online documentation at java.sun.com/docs. I use them all the time, you probably will find them useful as well. In fact, I would strongly suggest that you get very aquainted with the java.sun.com site in general. There is ALOT OF INFORMATION available to curious coders such as yourself.

    Because most of the objects that we used throw I/O Exceptions we have to catch those exceptions at the end of our program.

    I realize that this example is kind of all over the place, but It should illustrate some of the basics of File and User I/O in Java. Happy Coding!

  8. #8
    Hey, Thanks a lot "viCC". I do have reasonable familiarity with java. I do understand the stuff that you have posted. Thanks for your effort. I will definitely give it a go.
    Things that don\'t kill you make you stronger !

  9. #9
    Ok great, I couldn't really get an idea of how advanced you were so when in doubt, I try to explain everything out. Hope I didn't come off as patronizing. Have fun

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •