测试已正确,
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
// 新文件
File newFile = new File("c:\\newFile.txt");
int count = 0;
// 读取文件内容
File file = new File("c:\\file.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuffer sb = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
// 统计字母个数
for (int i = 0; i < sb.length(); i++) {
char ch = sb.charAt(i);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
count ++;
}
}
System.out.println(count);
FileOutputStream out = new FileOutputStream(newFile);
out.write(String.valueOf(count).getBytes());
out.flush();
out.close();
}
}