Java -- Need help to enhance the code

96 Views Asked by At

I wrote a simple program to read the content from text/log file to html with conditional formatting.

Below is my code.

import java.io.*;
import java.util.*;
class TextToHtmlConversion {
public void readFile(String[] args) {
for (String textfile : args) {
try{
      //command line parameter
      BufferedReader br = new BufferedReader(new FileReader(textfile));
      String strLine;
      //Read File Line By Line
      while ((strLine = br.readLine()) != null)   {
      Date d = new Date(); 
      String dateWithoutTime = d.toString().substring(0, 10);
      String outputfile = new String("Test Report"+dateWithoutTime+".html");
      FileWriter filestream = new FileWriter(outputfile,true);
      BufferedWriter out = new BufferedWriter(filestream);
      out.write("<html>");
      out.write("<body>");
      out.write("<table width='500'>");
      out.write("<tr>");
      out.write("<td width='50%'>");
      if(strLine.startsWith(" CustomerName is ")){
            //System.out.println("value of String split Client is :"+strLine.substring(16));
            out.write(strLine.substring(16));
            }
        out.write("</td>");
        out.write("<td width='50%'>");
            if(strLine.startsWith(" Logged in users are ")){
                if(!strLine.substring(21).isEmpty()){
                    out.write("<textarea name='myTextBox' cols='5' rows='1' style='background-color:Red'>");
                    out.write("</textarea>");
                    }else{
                  System.out.println("else if block:");
                  out.write("<textarea name='myTextBox' cols='5' rows='1' style='background-color:Green'>");
                  out.write("</textarea>");
                } //closing else block
              //out.write("<br>");
        out.write("</td>");   
            }
        out.write("</td>");
        out.write("</tr>");
        out.write("</table>");
        out.write("</body>");
        out.write("</html>"); 
     out.close();
    }
    //Close the input stream
    in.close();
 }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
            e.printStackTrace();
      }
    }
}

 public static void main(String args[]) {
     TextToHtmlConversion myReader = new TextToHtmlConversion();
 String fileArray[] = {"D:/JavaTesting/test.log"};
 myReader.readFile(fileArray);

  }
}

I was thinking to enhance my program and the confusion is of either i should use Maps or properties file to store search string. I was looking out for a approach to avoid using substring method (using index of a line). Any suggestions are truly appreciated.

1

There are 1 best solutions below

1
On

From top to bottom:

  • Don't use wildcard imports.
  • Don't use the default package
  • restructure your readFile method in more smaller methods
  • Use the new Java 7 file API to read files
  • Try to use a try-block with a resource (your file)
  • I wouldn't write continuously to a file, write it in the end
  • Don't catch general Exception
  • Use a final block to close resources (or the try block mentioned before)

And in general: Don't create HTML by appending strings, this is a bad pattern for its own. But well, it seems that what you want to do.

Edit

Oh one more: Your text file contains some data right? If your data represents some entities (or objects) it would be good to create a POJO for this. I think your text file contains users (right?). Then create a class called Users and parse the text file to get a list of all users in it. Something like:

List<User> users = User.parse("your-file.txt");

Afterwards you have a nice user object and all your ugly parsing is in one central point.