0

I have a text file of a story written out as each line of the story is one line, and I'm trying to write it to an HTML file using Java. I am unsure of how to do this, all I know is that I have to create a File object for the text file and a File object for the HTML file, and then open a printstream. But I'm still confused as to how to write from the input file to the output file using the printstream?

EDIT:

Certain lines need to have certain HTML tags, for example the top line of the textfile should have a header file, and then certain lines in all capital letters should have a header file, and then every other line should be surrounded by

tags.

I don't know of an efficient way to do this. Here's what I have so far:

private static String topstring = "<!doctype html>" + "\n" + "<html>" + 
                            "\n" + "<head>" + "\n" + 
                            "<meta charset=\"utf-8\">" + "\n" + 
                            "<title>My Web Page</title>" + "\n" + 
                            "</head>"+ "<body>";
public static void main(String[] args) throws IOException {
    File input = new File("story.txt");
    Scanner sc = new Scanner(input);
    File output = new File("newstory.html");
    PrintStream print = new PrintStream(output);
    print.println(top string);

And then after this I'm not sure what to do.

2 Answers 2

1

not sure on what basis/rules you would transfer txt file to html file. Assuming that you want to just dump the txt data to html data, I would give you hints what to do rather than writing the whole program for you.

  1. Create an object to read from txt file.
  2. Create an object to write to an html file.
  3. Add preliminary tags to your html file i.e.
<html>
<title>Your File title</title>
<body>
<p>
  1. Now create a while loop over txt file object. Read each line from this file in the loop. Check if line != null and then write it to the html file object wiht in the while loop.
  2. Once line == null it means you are done processing the txt file. Now its time to close the opened tags of html file i.e.,
</p>
</body>
</html>
  1. Now make sure to close both pointers.
Sign up to request clarification or add additional context in comments.

Comments

0

Why are you trying to output a HTML file like that? Isn't it going to be missing all the HTML tags?

The process would be the same as writing to any other text file.

To read one line from a txt file called 'hello' and print it to a HTML file called 'output':

File inputFile = new File("hello.txt"); Scanner sc = new Scanner(inputFile);

String line = sc.readLine();

File outputFile = new File("output.html") PrintStream ps = new PrintStream(outputFile);

ps.write(line);

You can use a loop to do this multiple times and therefore complete all lines in your input file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.