JojoMiss

疯魔jojo的后花园

导航

Java 读取MacOS 本地的 rtf 文件内容:中英文皆可

原是抄的大神的源码,东拼西凑的找了大半天,已忘记原链接了。特此声明⭐️本人不生产代码,只是代码的搬运工。

 

 /**
     * 用换行符讲读取的整个文档内容截取成若干字符串
     */
    public static String[] interfacePath;
    //    读取本地文件内容
    public static void chars() throws Exception {
        String str= readRtf();
        interfacePath = str.split("\n");
        for (i = 0; i < interfacePath.length; i++) {
            System.out.println(interfacePath[i]);//将被分割的字符串输出
        }
        return;
    }
    /**
     * 读取本地rtf文件
     */
    public static String readRtf() {
        String result = null;
        File file = new File(filePath);
        try {
            DefaultStyledDocument styledDoc = new DefaultStyledDocument();
            InputStream is = new FileInputStream(file);
            new RTFEditorKit().read(is, styledDoc, 0);
            result = new String(styledDoc.getText(0,styledDoc.getLength()).getBytes("ISO8859_1"),"GBK");
            //注意这边加上GBK即可;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

 

posted on 2024-04-02 22:32  幽忧一世  阅读(2)  评论(0编辑  收藏  举报

⭐️CSDN 我的文章列表⭐️
⭐WEIBO 微博首页⭐️
⭐园子 我的院子⭐️