We are going to write a simple program to convert a plain text file to HTML. To make life simpler, the program will assume the input file is
a.txt
and the output file is
a.html
. We will use Reader.java and Writer.java written previously. You may have a look at them first:
Defining the Problem
Before writing the program, we would try to do a manual conversion first. This help us understand what should be implemented. The program will read the content of
a.txt
in memory, do the conversion, and then write the content to
a.html
. Sample input and output follows.
Sample input file: a.txt
class A
{
public static void main(String[] args)
{
int b=10;
for (int i=0;i<b;i++)
System.out.println("Hello World !\nHello again !");
}
}
Sample output file: a.html
<html><body><pre style='font-family:monospace;font-size:12px'><code>
class A
{
public static void main(String[] args)
{
int b=10;
for (int i=0;i<b;i++)
System.out.println("Hello World !\nHello again !");
}
}
</code></pre></body></html>
Implementation Steps
1. Read the whole file
a.txt
into a String
2. Escape all special characters. That is :
Replace & with &
Replace < with <
Replace > with >
Replace " with "
3. Surround the content with appropriate HTML tags.
4. Write the content to
a.html
Note that overwrite mode is turned off by default. If you wish to overwrite existing file, modify
Writer.java
Source
The above steps lead to the following simple implementation.
No comments:
Post a Comment