Home Java Java-conversion Convert Text To Word

Related Tutorials


 
 

Share on Google+Share on Google+

Convert Text To Word

Advertisement
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.*;
import java.io.*;

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

Download of this program:

Output of this program.

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 10, 2007

Related Tutorials

Discuss: Convert Text To Word   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:11
hari
February 16, 2012
.doc,

need conversion
babp
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.
Aakash
July 17, 2012
Software Download

Soft
amir
August 1, 2012
image

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(); LOGGER.info("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()); LOGGER.info("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/vnd.ms-excel" ); } else if (fileType.trim().equalsIgnoreCase("pdf")) { response.setContentType( "application/pdf" ); LOGGER.info("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 = inStream.read(buf, 0, buf.length)) > 0) { LOGGER.info("size:"+sizeRead); outStream.write(buf, 0, sizeRead); } inStream.close();*/ // outStream.close(); outs.flush(); outs.close(); out.close(); out.flush(); } } catch (Exception e) { LOGGER.info("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="com.ibm.tool.command.Description"%> <%@page import="java.util.List, com.ibm.tool.command.Keywords;"%> <%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@taglib prefix="spring" uri="http://www.springframework.org/tags"%> <% String contextPath = request.getContextPath(); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <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%>/search.do?keepSearch=true">Back</a><br/></td> </tr> </table> </form:form> </div> </div> </body> </html>
reza
November 18, 2012
text to word

very nice
RAJENDRA K SHAH
October 30, 2013
DOCUMENT

document are very important
DMCA.com