Page 646 - Introduction to Programming with Java: A Problem Solving Approach
P. 646

                612 Chapter 15 Files
15.5 HTML File Generator
Now let’s look at an example that illustrates both input from a text file and output to a text file. The program in Figures 15.6a and 15.6b reads the contents of a user-specified text file. It translates that data into a Web page format. Then, it outputs the translation to a newly generated HTML file.
  /*******************************************************
*
HTMLGenerator.java
Dean & Dean
This program copies the contents of a user-specified
file and pastes it into a newly generated HTML file.
*******************************************************/
*
*
*
*
import java.util.Scanner;
import java.io.*;
public class HTMLGenerator
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
        Apago PDF Enhancer
// input file connection
// position of dot in filename
// HTML file's name
// HTML file connection
// a line from the input file
String filenameIn; // original file's name
Scanner fileIn;
int dotIndex;
String filenameOut;
PrintWriter fileOut;
String line;
System.out.print("Enter file's name: ");
filenameIn = stdIn.nextLine();
try This opens a file for input. {
fileIn = new Scanner(new FileReader(filenameIn));
// Compose the new filename
dotIndex = filenameIn.lastIndexOf(".");
if (dotIndex == -1) // no dot found
   {
}
{
}
filenameOut =
filenameIn.substring(0, dotIndex) + ".html";
filenameOut = filenameIn + ".html";
else // dot found
  fileOut = new PrintWriter(filenameOut);
This opens a file for output.
  Figure 15.6a HTMLGenerator program—part A
















































   644   645   646   647   648