|
count character in text file as input file
Hello,
I am a newbie... can anyone help me to figure out this code?
I want to count a character in the input text file and if they are more than 30 character..they will return a error message.
but my problem is.. it fuction only for the first line of the text file and pop the message. the go to the next line and pop the message.
how to make it function : read all the line in the input text file, if more than 30 char in that text file they will pop a message?
import java.io.*;
import javax.swing.JOptionPane;
public class Main {
static public String getContents(File aFile)throws FileNotFoundException, IOException {
if (aFile == null ) {
throw new IllegalArgumentException("File should not be null.");
}
if (!aFile.exists()) {
throw new FileNotFoundException ("File does not exist: " + aFile);
}
StringBuffer contents = new StringBuffer();
BufferedReader input = null;
try {
input = new BufferedReader( new FileReader(aFile) );
String line = null;
(( line = input.readLine()) != null){
long numChar = 0;
numChar += line.length();
JOptionPane.showMessageDialog(null,numChar,"Charac ter in the Message",JOptionPane.INFORMATION_MESSAGE);
contents.append(line);
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}
finally {
try {
if (input!= null) {
input.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
return contents.toString();
}
public static void main (String[] args) throws IOException {
File testFile = new File("key1.txt");
JOptionPane.showMessageDialog(null, getContents(testFile),"Original File",JOptionPane.INFORMATION_MESSAGE);
}
}
hmmm? or there is other suggestion?
|