Convert Text To Word

In this example, you will learn how to convert text to Word file.

Convert Text To Word


In this example, You will learn how to convert text to word file. Here, we are going to discuss about the conversion of text to word file. 

Core Description:

The following program uses two constructors for converting text to word file. This program converts the text to word file by using FileOutputStream("TextToWord.doc"). This program writes the content in word file using write() method of OutputStreamWriter object.

Here is the code of this program:

import java.math.*;

public class TextToWord
  public static void main(String arg[]){
  FileOutputStream fs = new FileOutputStream("TextToWord.doc");
  OutputStreamWriter out = new OutputStreamWriter(fs);  
  out.write("Welcome to RoseIndia!");
  catch (IOException e){

Download of this program:

Output of this program.



Share on Google+Share on Google+

Convert Text To Word

Posted on: July 10, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Convert Text To Word   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
February 16, 2012

need conversion
February 17, 2012
txt to word

personal file,
otos flournoy
March 31, 2012
problems opening the .doc in word

The .doc created by TextToWord program does not open Word like the .csv file opens in Excel "Help".
otis flournoy
March 31, 2012
text to word

The .doc does no open like the .csv file.
July 17, 2012
Software Download

August 1, 2012

I want to have a text copy paste the image into Word, I think I should Chkarknm
Kartik Chandra Mandal
September 26, 2012
File download by spring and hibernate and JSP

//@ResponseBody @RequestMapping(value = "/jobseekerDownloadCoverLetter", method = RequestMethod.GET) public ModelAndView jobseekerDownloadCoverLetter(HttpServletRequest request, HttpSession session, ResCoverLetterForm resCoverLetterForm, BindingResult result,HttpServletResponse response) { //ModelAndView model = new ModelAndView(); //String file = null; try { String covId=request.getParameter("coverletterId"); int coverletterId=Integer.parseInt(covId); ResCoverLetterDTO listOfCoverLetter=coverLetterService.getCoverList(coverletterId); if (listOfCoverLetter!=null){ resCoverLetterForm.setActive(listOfCoverLetter.getActive()); resCoverLetterForm.setCoverletterId(listOfCoverLetter.getCoverletterId()); resCoverLetterForm.setCoverletterText(listOfCoverLetter.getCoverletterText()); resCoverLetterForm.setName(listOfCoverLetter.getName()); resCoverLetterForm.setUserId(listOfCoverLetter.getUserId()); String fileName=""; fileName=listOfCoverLetter.getName();"Filename:"+fileName); String fName=fileName; FileOutputStream fs = new FileOutputStream(fName); OutputStreamWriter out = new OutputStreamWriter(fs); out.write(listOfCoverLetter.getName()); out.write(listOfCoverLetter.getCoverletterText()); response.setContentType( "application/msword"); response.setHeader("Content-Disposition","attachment; filename="+fName); response.setHeader("Cache-Control", "no-cache"); byte[] bytesGot = listOfCoverLetter.getCoverletterText().getBytes(); ServletOutputStream outs = response.getOutputStream(); outs.write(bytesGot); /* String filePath = "C:\\test reference-material"; File f=new File(filePath, fileName); String fileType = fileName.substring(fileName.indexOf(".")+1,fileName.length());"Filetype:"+fileType+";"+f.length());*/ // if (fileType.trim().equalsIgnoreCase("txt")) { // response.setContentType( "text/plain" ); /* } else if (fileType.trim().equalsIgnoreCase("doc")) { response.setContentType( "application/msword" ); } else if (fileType.trim().equalsIgnoreCase("xls")) { response.setContentType( "application/" ); } else if (fileType.trim().equalsIgnoreCase("pdf")) { response.setContentType( "application/pdf" );"content type set to pdf"); } else { response.setContentType( "application/octet-stream" ); } */ // response.setContentLength((int)f.length()); /* PrintWriter out = response.getWriter(); out.write(fileName); out.write("\n"); out.write(listOfCoverLetter.getCoverletterText()); String fName=fileName+".txt";*/ // response.setHeader("Content-Disposition","attachment; filename="+fName); // response.setHeader("Cache-Control", "no-cache"); /* byte[] buf = new byte[8192]; FileInputStream inStream = new FileInputStream(f); int sizeRead = 0; while ((sizeRead =, 0, buf.length)) > 0) {"size:"+sizeRead); outStream.write(buf, 0, sizeRead); } inStream.close();*/ // outStream.close(); outs.flush(); outs.close(); out.close(); out.flush(); } } catch (Exception e) {"This is Account Addresss edite option error"+e); } return new ModelAndView(); } and JSP file is <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import=""%> <%@page import="java.util.List,;"%> <%@taglib prefix="form" uri=""%> <%@taglib prefix="spring" uri=""%> <% String contextPath = request.getContextPath(); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>SearchResult Page</title> </head> <body> <% Description desc = (Description)request.getAttribute("commandBean"); %> <div style="border-style: solid; border-width: 3px; margin-top: 20px; margin-bottom: 20px; "> <div style="background-color: #303030"> <table width="100%"> <tr> <th align="left"><font size="5px" color="#D1D1D1">Search Result(s)</font></th> </tr> </table> </div> <div style="margin-bottom: 10px; margin-left: 5px; margin-top: 10px"> <form:form method="POST" commandName="commandBean" id="searchForm"> <table border="1" bordercolor="#424242" width="980" height="200"> <tr> <th align="left" bgcolor="#8F8F8F" colspan="1"><font size="4px" style="font-family: sans-serif">Problem Statement </font></th> <% if(desc.getProblemId()>=0 ){ %> <td colspan="3"><%=desc.getProblemText().trim() %></td> <%} %> </tr> <tr> <th align="left" bgcolor="#8F8F8F" colspan="1"><font size="4px" style="font-family: sans-serif">Solution</font></th> <% if(desc.getProblemId()>=0){ %> <td colspan="3"><pre><%=desc.getSolutionText().trim() %></pre></td> <%} %> </tr> <tr> <th align="left" bgcolor="#8F8F8F" colspan="1"><font size="4px" style="font-family: sans-serif">External Link</font></th> <% if(desc.getProblemId()>=0){ if(desc.getExternalLinkList().size() > 0){ %> <td colspan="3"> <% for(int k=0;k<desc.getExternalLinkList().size();k++){ %> <font size="2" style="font-family: sans-serif"><a href="http://<%=desc.getExternalLinkList().get(k).trim() %>"><%=desc.getExternalLinkList().get(k) %></a></font><br/> <%}%> </td><%} }%> </tr> <% if(desc.getProblemId()>=0){ if(desc.getExtension()!=null){ %> <tr> <th align="left" bgcolor="#8F8F8F" colspan="1"><font size="4px" style="font-family: sans-serif">Attachement</font></th> <td><a href="<%=contextPath%>/jobseekerDownloadCoverLetter.html?coverletterId=<%=desc.getProblemId() %>">Download File</a></td> </tr> <%}} %> <tr> <td colspan="4"><br/><a href="<%=contextPath%>/">Back</a><br/></td> </tr> </table> </form:form> </div> </div> </body> </html>
November 18, 2012
text to word

very nice
October 30, 2013

document are very important