Reading an RTF file in Java

Hi,
Can someone tell me how can we read the data from an .rtf files like we do with text files in Java.
I have seen solutions using Swing rtfeditor etc.. But I just need to read the data, I do not want to open rtf page.
Any help would be appreciated.
KK

If you are not to "read" but to obtain bytes then you can modify this:
//try{
  InputStream stream = new FileInputStream("cc.rtf");//file name is arbitrary
                                                                         //or BufferedInputStream can be used
   int n=0;
while((n=stream.read())!=-1){
            System.out.print(Integer.toString(n));
            System.out.print(" ");
    System.out.println();
   if(stream!=null) stream.close();
//   catch(IOException ioe){
//       throw ioe;
//  }

Similar Messages

  • How To Read RTF file in JAVA?  Using  iText?

    How To Read RTF file in JAVA?  Using  iText?.....
    import java.io.*;
    import com.lowagie.text.*;
    import com.lowagie.text.rtf.*;
    public class RTF3 {
    public static void main(String[] args) {
    // System.out.println("This example generate a RTF file name Sample.rtf");
    // Create Document object
    Document myDoc = new Document();
    try {
    // Create writer to listen document object
    // and directs RTF Stream to the file Sample.rtf
    RtfWriter2.getInstance(myDoc, new FileOutputStream("Sample.rtf"));
    // open the document object
    myDoc.open();
    // Create a paragraph
         Paragraph p = new Paragraph();
         p.add("Helloworld in Rtf file..amazing isn't");
         // Add the paragraph to document object
    myDoc.add(p);
    catch(Exception e) {
    System.out.println(e);
    //close the document
    myDoc.close();
    Exception in thread "main" java.lang.NoSuchMethodError: com.lowagie.text.Rectangle.width()F
         at com.lowagie.text.rtf.document.RtfPageSetting.rectEquals(RtfPageSetting.java:433)
         at com.lowagie.text.rtf.document.RtfPageSetting.guessFormat(RtfPageSetting.java:362)
         at com.lowagie.text.rtf.document.RtfPageSetting.setPageSize(RtfPageSetting.java:341)
         at com.lowagie.text.rtf.RtfWriter2.setPageSize(RtfWriter2.java:248)
         at com.lowagie.text.Document.open(Unknown Source)
         at view.RTF3.main(RTF3.java:23)
    CAN you HELP me?

    import com.lowagie.text.Document;
    import com.lowagie.text.rtf.parser.RtfParser;
    import java.io.FileInputStream;
    String inputFile = "sample.rtf";
    Document document = new Document();
    document.open();
    RtfParser parser = new RtfParser(null);
    parser.convertRtfDocument(new FileInputStream(inputFile), document);

  • Why doesn't read the RTF file?

    Hello,
    Could anyone tell me what is the problem with the following code. It would
    read an 'rtf' file but it doesn't.
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.text.rtf.*;
    import java.io.*;
    import java.awt.event.*;
    public class Rtf extends JFrame {
    String filein = System.getProperty("user.dir")
    + System.getProperty("file.separator")
    + "BG.rtf";
    public Rtf() {
         RTFEditorKit rtfKit = new RTFEditorKit();
         DefaultStyledDocument doc = new DefaultStyledDocument();
         try {
              rtfKit.read( new FileInputStream( filein ), doc, 0 );
         } catch(Exception e) {
              System.out.println("Read error: " + e);
         System.out.println("doc.getLength: " + doc.getLength());
    public static void main(String[] args) {
    JFrame frame = new Rtf();
    During the execution the following lines are the result:
    Read error: java.lang.NullPointerException
    doc.getLength: 0
    Press any key to continue...
    Thank you in advance.

    If your rtf is made in word 2000 or newer version, rtf kit cannot read it and return null pointer statement

  • Creation of .RTF file in java

    Hi,
    I want to create/edit .RTF file through java application. In my RTF file, I should be able to edit data(formatting), inserting tables. This is already done. Now I want to create chart (like pie-chart) using java. I searched the net for the last couple of days but I couldn't get the proper help. I can do that in SWING but its of no use.
    can anyone help me in this?
    Thanks & Regards,
    mrP

    Hi,
    Can someone please point me in the right direction? I'm trying to create an RTF file from data in a database, so I have no need to use JTextPanel or any GUI components. I've been trying to use the RTFEditorkit with a DefaultStyledDocument, but can only manage to insert one line into the document(!) Does anyone have any suggestions or examples that would explain how to do this?
    Any ideas would be appreciated.
    Thanks in advance,
    Gillian

  • Printing problem with RTF file in java

    Hi,
    i actually got code to print the RTF doc using java, it works fine if we give small content (some text), but throws error 'java.io.ioException: Too many close-groups in RTF " when actual bill content is pass.
    Thanks in advance

    Can you please share the code to print the RTF file using java API

  • How to read a text file using Java

    Guys,
    Good day!
    Please help me how to read a text file using Java and create/convert that text file into XML.
    Thanks and God Bless.
    Regards,
    I-Talk

         public void fileRead(){
                 File aFile =new File("myFile.txt");
             BufferedReader input = null;
             try {
               input = new BufferedReader( new FileReader(aFile) );
               String line = null;
               while (( line = input.readLine()) != null){
             catch (FileNotFoundException ex) {
               ex.printStackTrace();
             catch (IOException ex){
               ex.printStackTrace();
         }This code is to read a text file. But there is no such thing that will convert your text file to xml file. You have to have a defined XML format. Then you can read your data from text files and insert them inside your xml text. Or you may like to read xml tags from text files and insert your own data. The file format of .txt and .xml is far too different.
    cheers
    Mohammed Jubaer Arif.

  • How to Read a CAB File from JAVA?

    Hi,
    Anyone knows how to read a CAB File from java. I need to read a property file of txt file that is packaged in CAB file & then based on that, I have to do processing. Is there anyway to do it.
    I had tried using java.util.zip.ZipFile Class, but it does work for JAR, but not for CAB.
    siva.

    Perhaps there's something in the Cabinet SDK that will help:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncabsdk/html/cabdl.asp

  • How to read an ARPA file in java

    Hi
    Im new in java and dont know many thing about it, I want to know how can read an ARPA file in java.
    pleas describ it step bye step and esy to understand.
    Edited by: 836719 on Feb 14, 2011 8:15 PM

    836719 wrote:
    ok i explain what i want to do maybe u can help me more,
    i want to read an arpa file and search it to find for example a word.so u think which method that u mentioned above is beter???
    It will be pleasure if u have any tip that can help me.1. Learn the basics of programming as well as how to logically construct and deconstruct a problem domain.
    2. Learn the basics of programming java.
    3. Learn about java.io.*
    4. Learn about java swing
    5. Find documentation that describes the format of the ARPA file.
    6. Read the documentation and understand it.
    7. Find a source for ARPA files (if this is online then there is more java to learn as well.)
    8. Use 1,2 and 3 to write java code that consumes the ARPA file.
    9. Use 4 to ask the user for a word.
    10. Use 1 and 2 to write code that 'searches' what you read in 8.

  • How to read and XSD file in java prospective in NWDS

    Hi !!
    anybody can help me that
    how to read and XSD file in java prospective in Netweaver developer Studio
    Regards
    Abhishek Agrahari

    I guess you can make it more clear...

  • How to convert .doc file into .rtf file in Java?

    Hello All,
    I want to convert doc file into rtf format in java and for the same i am not getting any help so pls suggest some solution for that.
    Thanks and Regards
    only1Vinay

    MS-Word formats (DOC) are notorious for not being standardized from one version to another, so what ever you get will be version specific. If you must do the conversion, I suggest you do a MS-Script in Word to do it or one of the .Net languages. As stated the Word format from version to version is not standardized.

  • Printing RTF files in Java

    Hi,
    I have a java application (No GUI) that needs to print an RTF file. Could any one provide me links to tutorial/sample codes for doing this?
    Thanks a lot,
    Vaishali

    Look at http://www.fawcette.com/javapro/2002_12/online/print_kgauthier_12_10_02/default_pf.aspx. They provide an utility class which basically lets you do this in 1 line, by creating a JEditorPane with your document and passing it to a method there.

  • Printing RTF files from Java

    Hello,
    I need to print RTF files from a java program. I am reading RTF files in my program and after modification need to send them to a network printer for printing.
    Please let me know if there are any tutorials/books available on this.
    Thank you very much,
    Regards,
    Vaishali

    check these urls. may help u some extent.
    http://java.sun.com/docs/books/tutorial/2d/printing/index.html
    http://www.javaworld.com/javaworld/jw-03-2001/jw-0302-print.html
    http://java.sun.com/products/java-media/2D/forDevelopers/sdk12print.html

  • Merging Word (RTF) files in java

    Hey,
    I have a requirement to merge several word (or rft) files in java. The files are stored as blobs on the database. I had a look at http://poi.apache.org/hwpf/docoverview.html but that project seems to be parked at the moment. Has anybody any ideas?
    Thanks

    I agree its a bad idea but i was asked to look into
    its possibility.
    Previously it wasn't a problem as it was always one
    word file that was required
    for editing through the browser by the user but now
    they want to basically build a 'master doc' based on
    a bunch of business rules i.e. chose a subset of all
    their templates based on business rules at runtime.
    I pitched the idea of generating XSL:FO and adding
    conditionals around the various templates and then
    generating RTF from this. Storing the XSL:FO on the
    db but they didn't like that as they want to be able
    to add and modify new templates and then store these
    directly on the DB?!?!?!The way to address this is to find out what the real concerns are. Almost all stupid requirements are based upon some concern that is the result of a combination of fear and ignorance.
    So I would suggest you find out what the real concern is and address that specifically in your better design.
    Because the reality is that your solution is actually workable. Whereas the one you are being pushed towards will be very difficult and I fear result in an unmaintable mess.

  • Is there any way to read the pdf file in java

    Hai,
    i need some advice from anyone that is there anyway to read the pdf file directly from java...
    Without converting to other files(txt,html)
    if it is possible please help me..
    Thank you

    Or use the Adobe one (search the adobe site). Or search this forum, this is one of the Top Ten Qestions Asked here.

  • Can't read a text file using java

    Hi All
    I am trying to read a log file.
    ProgramA keeps updating the log file, while my program ProgramB reads it.
    For some readson my ProgramB is not able to read the last two lines in the log file. If I run the program in debug mode it is reading all the lines.
    This is having me frustrated.
    Please let me know if there is a way to read entire contents.
    Here is how I am reading the files ( 2ways)
         private static String readFileAsString(String filePath)
        throws java.io.IOException{
            StringBuffer fileData = new StringBuffer(1000);
            FileReader fr = new FileReader(filePath);
            BufferedReader reader = new BufferedReader(fr);
            char[] buf = new char[1024];
            int numRead=0;
            while((numRead=reader.read(buf)) != -1){
                String readData = String.valueOf(buf, 0, numRead);
                fileData.append(readData);
                buf = new char[1024];
            reader.close();
            fr.close();
            return fileData.toString();
           * Fetch the entire contents of a text file, and return it in a String.
           * This style of implementation does not throw Exceptions to the caller.
           * @param aFile is a file which already exists and can be read.
           static public String readFileAsString(String filePath) {
             //...checks on aFile are elided
             StringBuffer contents = new StringBuffer();
             //declared here only to make visible to finally clause
             BufferedReader input = null;
             try {
               //use buffering, reading one line at a time
               //FileReader always assumes default encoding is OK!
               input = new BufferedReader( new FileReader(filePath) );
               String line = null; //not declared within while loop
               * readLine is a bit quirky :
               * it returns the content of a line MINUS the newline.
               * it returns null only for the END of the stream.
               * it returns an empty String if two newlines appear in a row.
               while (( line = input.readLine()) != null){
                 contents.append(line);
                 contents.append(System.getProperty("line.separator"));
             catch (FileNotFoundException ex) {
               ex.printStackTrace();
             catch (IOException ex){
               ex.printStackTrace();
             finally {
               try {
                 if (input!= null) {
                   //flush and close both "input" and its underlying FileReader
                   input.close();
               catch (IOException ex) {
                 ex.printStackTrace();
             return contents.toString();
           }

    If you're using JDK 5 or later, you could use the Scanner class for input information. See below:
    try {
                   Scanner reader = new Scanner(new File("C:\\boot.ini"));
                   StringBuilder sb = new StringBuilder();
                   while (reader.hasNextLine()) {
                        sb.append(reader.nextLine());
                        sb.append(System.getProperty("line.separator"));
                   System.out.println(sb.toString());
              } catch (FileNotFoundException e) {
                   e.printStackTrace();
              }

Maybe you are looking for

  • DUMP in MFBF transcation

    Hi Folks, Users getting the frequent dumps in production systems. actaully we observed that dumps are coming when the users accessing the MFBF transaction manually when the batch job was running for backflush and NRIV (number ranges) table is getting

  • Module pool help needed

    Please use a more informative subject in future hi Experts In my module pool program i having a list box in a modeled dialog box . if the list box is left empty it will give an error message .after pressing the enter buttton the list box gets disable

  • Navigate using back button.

    Hi I want to refresh the page, and start it as a new request if back button is pressed by the user. Is this possible ? Waiting for response. Thanks in advance. Regards. Ranjan

  • Flash Content error......

    Hi I found the error popup windows "Flash content error : 9" is displayed when swf file is played in Flash Lite 2.0 on Symbian OS. What is this error popup mean?? And how can I solve this problem?? Please, help me.... Thank you.

  • [solved] Lost surround 5.1 with kernel version 3.3.1

    Hi!  Since i've upgrade to kernel 3.3.1, i lost surround sound, even if pulseaudio says that surround 5.1 is enable. My modprobe.d dir contains a special file (alsa-base.conf) that contains theses lines: alias snd-card-0 snd-hda-intel options snd-hda