编写程序查找java源文件里的关键字个数
老师布置的作业
话不多说直接上代码
有意见或建议欢迎大家指出
这个由于用了fx所以我建的fx模块
FileReader类主要进行读取文件
package sample;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class FileReader {
/*从源文件上读取的字符串*/
private String sourceFile;
/*从关键字文件上读取的字符串*/
private String keyWords;
public FileReader() {
while (this.keyWords == null || this.sourceFile == null) {
if (this.keyWords == null) {
Label label = new Label("please choose JAVA source file");
try {
readFile("keyWords");
} catch (IOException e) {
e.printStackTrace();
}
} else if (this.sourceFile == null) {
Label label = new Label("please choose JAVA source file");
try {
readFile("source");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*获取文件路径*/
private static String getFilePath() {
int result = 0;
File file = null;
String path = null;
JFileChooser fileChooser = new JFileChooser();
FileSystemView fsv = FileSystemView.getFileSystemView(); //注意了,这里重要的一句
System.out.println(fsv.getHomeDirectory()); //得到桌面路径
fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
fileChooser.setDialogTitle("请选择要上传的文件...");
fileChooser.setApproveButtonText("确定");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
result = fileChooser.showOpenDialog(null);
if (JFileChooser.APPROVE_OPTION == result) {
path = fileChooser.getSelectedFile().getPath();
System.out.println("path: " + path);
}
return path;
}
public String getSourceFile() {
return sourceFile;
}
public String getKeyWords() {
return keyWords;
}
/*获取字符串*/
private void readFile(String selction) throws IOException {
System.out.println("请选择输入文件地址或自行选择文件\n输入地址请按1\n自己选择请按2");
int choose = 0;
String path = "";
Scanner input = new Scanner(System.in);
while (choose != 1 && choose != 2) {
choose = input.nextInt();
}
if (choose == 1) {
path = input.next();
} else if (choose == 2) {
path = getFilePath();
}
if (path != null) {
StringBuilder text = new StringBuilder();
try {
FileInputStream inputStream = new FileInputStream(path);
byte[] bytes = new byte[1024];
int length = 0;
while ((length = inputStream.read(bytes)) != -1) {
text.append(new String(bytes, 0, length));
}
inputStream.close();
} catch (FileNotFoundException e) {
System.out.println(e);
e.printStackTrace();
}
String s = text.toString();
if (selction == "source") {
this.sourceFile = s;
} else {
this.keyWords = s;
}
}
TODO: 2021/3/9 未选择文件
else {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setContentText("未选择文件");
alert.show();
// return "";
}
}
}
Matcher类
package sample;
import java.util.HashMap;
import java.util.Iterator;
public class Matcher {
HashMap<String, Integer> keyWords = new HashMap();
public Matcher() {
FileReader fileReader = new FileReader();
recordKeyWord(fileReader.getSourceFile(), ToArray(fileReader.getKeyWords()));
}
private static int filter(String s, String sub) {
int old_length = s.length();
String replace = "";
if (s.contains(sub)) {
replace = s.replace(sub, "");
} else {
return 0;
}
int new_length = replace.length();
int count = (old_length - new_length) / (sub.length());
return count;
}
public HashMap getKeywords() {
return keyWords;
}
/*计算所有关键字总数*/
public int countAll() {
int num = 0;
Iterator iterator = keyWords.keySet().iterator();
while (iterator.hasNext() == true) {
num += keyWords.get(iterator.next());
}
return num;
}
private void recordKeyWord(String a, String[] b) {
int count = 0;
while (b[count] != null) {
count++;
}
for (int i = 0; i < count; i++) {
this.keyWords.put(b[i], filter(a, b[i]));
}
}
/*提取关键字数组*/
private String[] ToArray(String s) {
int head = 0;
int count = 0;
String keyWord = "";
char[] letter = s.toCharArray();
String[] keyWords = new String[100];
for (int i = 0; i < letter.length - 1; i++) {
if (letter[i] == ' ') {
for (int j = head; j <= i; j++) {
keyWord = keyWord + letter[j];
}
keyWords[count++] = keyWord;
keyWord = "";
head = i + 1;
}
}
return keyWords;
}
}
Main类
package sample;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Matcher matcher = new Matcher();
System.out.println("共" + matcher.countAll() + "个关键字");
System.out.println("其中" + matcher.keyWords);
}
}