import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VerifyIsEnglishCode
{
private static final File FILE_PATH = new File("file");
private List<File> fileList;
private File logFile;
private StringBuffer errorMessage;
public VerifyIsEnglishCode()
{
fileList = Arrays.asList(FILE_PATH.listFiles());
errorMessage = new StringBuffer();
getFileList();
}
public void getFileList()
{
for (File file : fileList)
{
try
{
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String fileLineContent = "";
int lineCount = 0;
while ((fileLineContent = bufferedReader.readLine()) != null)
{
lineCount++;
verifyMessage(file, fileLineContent, lineCount);
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
if (errorMessage.length() > 0)
{
try
{
writeErrorMessge();
} catch (IOException e)
{
e.printStackTrace();
}
} else
{
System.out.println("OK,没有发现非英文字符");
}
}
private void writeErrorMessge()
throws IOException
{
System.out.println(errorMessage.toString());
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy_dd_MM_HH_mm_ss");
logFile = new File("log/" + bartDateFormat.format(new Date()) + ".txt");
logFile.createNewFile();
FileWriter fileWriter = new FileWriter(logFile);
fileWriter.write(errorMessage.toString());
fileWriter.close();
}
private void verifyMessage(File file, String fileLineContent, int lineCount)
{
Pattern pattern = Pattern.compile("[^a-zA-Z0-9\\s=.,\\\"\\\\?!:~`@#$%^&*()_\\-+';<>/]");
Matcher matcher = pattern.matcher(fileLineContent);
if (matcher.find() && fileLineContent.trim().length() > 0)
{
errorMessage.append(file.getAbsolutePath()).append("\t")
.append(lineCount).append("\t\t")
.append(fileLineContent).append("\n");
}
}
public static void main(String[] args)
{
new VerifyIsEnglishCode();
}
}