trying to count characters in a .txt file. using eclipse 3.3.
package readfile;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
public class read {
char[] dict = new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' '};
int[] freq;
BufferedReader openFile(String filename){
try{
Reader fsrc = new FileReader(filename);
BufferedReader src = new BufferedReader( fsrc );
return src;
}
catch(IOException e){
System.out.println("File not found "+ e.getMessage());
return null;
}
}
public void print(String htmlfile) throws IOException{
File f = new File(htmlfile);
FileWriter fw = new FileWriter(f);
PrintWriter pw = new PrintWriter(fw);
pw.println("<html>");
pw.println("<head>");
pw.println("<title>");
pw.println("Java and HTML Exercise");
pw.println("</title>");
pw.println("</head>");
pw.println("<body>");
pw.println("<br/>");
pw.println("<br/>");
pw.println("<div align=center>");
pw.println("<h1> File Writing using Java</h3>");
pw.println("<br/>");
pw.println("<h2> Hello, my name is Gautam.</h3>");
pw.println("<br/>");
pw.println("<h3> This program writes the HTML file using java.</h3>");
pw.println("</div>");
pw.println("</body>");
pw.println("</html>");
pw.close();
}
public read(String filename) {
BufferedReader src = openFile(filename);
freq =new int [dict.length];
try{
String line = null;
while( (line = src.readLine()) != null) {
act(src,line);
}
}
catch(IOException e){
System.out.println("File read error "+ e.getMessage());
}
}
void act(BufferedReader src,String line) throws IOException
{
char[] arrayOfChars = line.toCharArray();
for ( int i=0; i<line.toCharArray().length;i++ ) {
for ( int j=0; j<line.toCharArray().length;j++ ) {
if(arrayOfChars=dict[j]){ // ERROR : cannot convert from char to boolean
// some code needs to be here to increment freq[] so that a table could be printed later
// not sure if this one is right
// int count = 0;
// freq[i]=count+1;
}
}
}
}
public static void main(String[] args) throws IOException {
read r = new read("test.txt");
r.print("htmlfile.html");
}
}