教学立方挂了,由于要录作业讲解视频,恰好之前下载过同学们的作业,所以直接读文件找满分作业比较容易,就参考了代码写了一下。
https://blog.csdn.net/navi617211950/article/details/52540364
https://blog.csdn.net/tian_sweety/article/details/81871864
两个来源
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
public class ReadRTF {
public static void getAllFileName(String path, ArrayList<String> listFileName) {
File file = new File(path);
String[] names = file.list();
if (names != null) {
String[] completNames = new String[names.length];
for (int i = 0; i < names.length; i++) {
completNames[i] = path + "\" + names[i];
}
listFileName.addAll(Arrays.asList(completNames));
}
}
public static String getTextFromRtf(String filePath) {
String result = null;
File file = new File(filePath);
try {
DefaultStyledDocument styledDoc = new DefaultStyledDocument();
// 创建文件输入流
InputStream streamReader = new FileInputStream(file);
new RTFEditorKit().read(streamReader, styledDoc, 0);
result = new String(styledDoc.getText(0, styledDoc.getLength()).getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
} catch (IOException | BadLocationException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws IOException {
ArrayList<String> listFileName = new ArrayList<>();
getAllFileName("D:\...\数据库开发技术_21 课后思考题_02月23日12时16分", listFileName);
for (String name : listFileName) {
if (name.contains(".rtf")) {
String result = getTextFromRtf(name);
if(result.contains("教师打分:5分")) {
System.out.println(result);
}
}
}
}
}