2013-10-04 11 views
0

8 비트 형식입니다 ....하지만 난이 코드 예를 들어 오류가 발생했습니다하는 javax.swing.text.rtf.RTFEditorKit는/RTF 내가 HTML2RTF와 내가 코드를 따라 실행하려고 에 대한 간단한 컨버터를 필요

코드 :

import java.io.ByteArrayInputStream; 
import java.io.StringWriter; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

import javax.swing.text.Document; 
import javax.swing.text.html.HTMLEditorKit; 
import javax.swing.text.rtf.RTFEditorKit; 

public class converter { 
    private static Pattern htmlTrimPattern = Pattern.compile(".*<body>(.*)</body>.*", Pattern.DOTALL); 

    public static void main(String[] args) 
    { 


     toRtf("<!DOCTYPE html>\n" + 
       "<html>\n" + 
       "<body>\n" + 
       "\n" + 
       "<h1>My First Heading</h1>\n" + 
       "\n" + 
       "<p>My first paragraph.</p>\n" + 
       "\n" + 
       "</body>\n" + 
       "</html>\n"); 

    } 

    public static String toRtf(String html) { 
     ByteArrayInputStream input= new ByteArrayInputStream(html.getBytes()); 
     StringWriter writer = new StringWriter(); 
     try { 
      RTFEditorKit rtfEditorKit = new RTFEditorKit(); 
      HTMLEditorKit htmlEditorKit = new HTMLEditorKit(); 
      Document htmlDoc = htmlEditorKit.createDefaultDocument(); 
      htmlEditorKit.read(input, htmlDoc, 0); 
      rtfEditorKit.write(writer, htmlDoc, 0, htmlDoc.getLength()); 
     } catch (Exception ex) { 
      System.out.println("Error"+ex); 
     } 
     System.out.println(writer.toString()); 
     return writer.toString(); 
    } 
} 

오류 : 내가 잘못

Errorjava.io.IOException: RTF is an 8-bit format 

을 뭐하는 거지? Google은이 경우 내 친구가 아니며 당신이 나를 도울 수 있기를 바랍니다.

답변

6

RTF은 8 비트 형식이지만 내용을 문자 스트림에 쓰는 클래스 인 Writer으로 보냅니다. "

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
… 
rtfEditorKit.write(baos, htmlDoc, 0, htmlDoc.getLength()); 
… 
System.out.println(baos.toString()); 

콘솔을하십시오 ByteArrayOutputStream 소요 write()의 변화를 시도

 
{\rtf1\ansi 
{\fonttbl\f0\fnil Monospaced;} 

\par 
My First HeadingMy first paragraph.\par 
}