java代码实现. 编写一个程序,程序实现对用户指定的文本文件中的英文字符和字符串 的个数进行统计的功能,并将结果根据用户选择输出至结果文件或屏幕。 第 5 / 6 页 1) 构建统计类,该类实现对 I/O 的操纵;实现从“file.tst”文本文件中读取英文字符、字符 串并统计;在“file2.txt"文本文件实现对统计结果的输出。 2) 构建测试类,该类实现与用户的交互,向用户提示操作信息,并接收用 户的操作请求。
时间: 2024-02-18 14:02:06 浏览: 25
下面是一个简单的 Java 代码实现,可以实现对指定文本文件中英文字符和字符串的统计,并根据用户选择将结果输出至结果文件或屏幕:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class FileStatistics {
private String inputFileName; // 输入文件名
private String outputFileName; // 输出文件名
private int charCount; // 英文字符个数
private int wordCount; // 英文单词个数
// 构造方法,初始化输入文件名和输出文件名
public FileStatistics(String inputFileName, String outputFileName) {
this.inputFileName = inputFileName;
this.outputFileName = outputFileName;
}
// 统计英文字符和单词
public void count() {
try (BufferedReader reader = new BufferedReader(new FileReader(inputFileName))) {
String line;
while ((line = reader.readLine()) != null) {
for (char c : line.toCharArray()) {
if (isEnglishChar(c)) {
charCount++;
}
}
String[] words = line.trim().split("\\s+");
for (String word : words) {
if (isEnglishWord(word)) {
wordCount++;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 判断是否为英文字符
private boolean isEnglishChar(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
// 判断是否为英文单词
private boolean isEnglishWord(String word) {
for (char c : word.toCharArray()) {
if (!isEnglishChar(c)) {
return false;
}
}
return true;
}
// 输出统计结果
public void output() {
try {
PrintWriter writer;
if (outputFileName != null) {
writer = new PrintWriter(new FileWriter(outputFileName));
} else {
writer = new PrintWriter(System.out);
}
writer.println("字符数:" + charCount);
writer.println("单词数:" + wordCount);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 测试程序
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入要统计的文件名:");
String inputFileName = input.nextLine();
System.out.print("请输入输出文件名(直接回车表示输出到屏幕):");
String outputFileName = input.nextLine();
FileStatistics fs = new FileStatistics(inputFileName, outputFileName.isEmpty() ? null : outputFileName);
fs.count();
fs.output();
input.close();
}
}
```
该程序分为两个类:`FileStatistics` 和测试类 `Main`。其中,`FileStatistics` 类实现了对文本文件中英文字符和单词个数的统计,包括从文件中读取数据和输出结果到文件或屏幕。测试类 `Main` 则负责与用户交互,接收用户的操作请求。
可以通过以下方式来测试该程序:
1. 将上面的代码保存为 `FileStatistics.java` 文件;
2. 打开命令行窗口,进入到代码所在的目录;
3. 编译代码:`javac FileStatistics.java`;
4. 运行程序:`java FileStatistics`;
5. 根据程序提示,输入要统计的文本文件名和输出文件名(可以直接回车表示输出到屏幕);
6. 等待程序运行完毕,查看统计结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)