읽기 RTF 파일

교육 큐브 그렇게 작업 쉽게 찾아 직접 파일을 읽고, 학생들이 작업을 다운로드하기 직전에, 동작을 설명하기 때문에 기록 비디오, 전화를 끊었, 우리는 참조 코드에 대해 썼다.

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);
                }
            }
        }
    }

}

추천

출처www.cnblogs.com/angelica-duhurica/p/12380832.html