• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File Convertion

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Good Morning everybody

I would like to convert .rtf file to .txt file by java code , please give the solution

Thanks
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java-code-converting-RTF-to-text - a sample code discussed about converting a RTF document to Text output.




RTF to Text conversion using Java swing packages.

This is an example about how to read a RTF document
and printing out all the texts of this RTF file,
as plain text.

I have used packages like javax.swing.text,
javax.swing.text.rtf from Java API.

RTF2Text.java
/**
* This code is provided "AS IS", without warranty of any kind.
* This is a sample code, might be not completely error free.
* This site or author of this code doesn't take any responsibility
* what so ever resulting out of usage of this code.
*/

public class RTF2Text
{
public RTF2Text(String fileName)
{
try{
//Reading the RTF file as data input stream
FileInputStream fin = new FileInputStream(fileName);
DataInputStream din = new DataInputStream(fin);

//creating a default blank styled document
DefaultStyledDocument styledDoc = new DefaultStyledDocument();

//Creating a RTF Editor kit
RTFEditorKit rtfKit = new RTFEditorKit();

//Populating the contents in the blank styled document
rtfKit.read(din,styledDoc,0);

// Getting the root document
Document doc = styledDoc.getDefaultRootElement().getDocument();

//Printing out the contents of the RTF document as plain text
System.out.println(doc.getText(0,doc.getLength()));

}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String args[])
{
new RTF2Text(args[0]);
}

}
----------------------------------------------------------------------

Now this example requires a RTF document, and by compiling this example
RTF2Text.java, and running with a command like:

java RTF2Java myrtffile.rtf

Output will be printed out on screen.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic