Home > Domino Tips > Developer > Java > Java code to write to text file
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

JAVA

Java code to write to text file


Michael Marcavage
10.28.2005
Rating: -2.71- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


This Java code will walk a Lotus Notes view, grab data from each document and write it to a text file, then send an e-mail to the appropriate contact stating whether the code was successful or if it failed.

import lotus.domino.*;
import java.io.*;
import java.util.*;
import java.util.Vector;

public class WriteTextFile extends AgentBase {
    private MIMEEntity mime;
    private int byteNumber;
    private int count;
    private byte byteArray[];
    
 public void NotesMain() {  
  try {
     Session session = getSession();
     AgentContext agentContext = 
session.getAgentContext();
     Database db = 
agentContext.getCurrentDatabase();
                          View view = 
db.getView("LKAddressLength");
                          Document doc = 
view.getFirstDocument();
                 
FileOutputStream fout;
fout = new FileOutputStream
 ("\\Server Name>\<Folder 1>
\<Folder 2>\DOCTOR.txt");
                 
                 while (doc != null) 
                            {        
String strDRPracticeName =
 doc.getItemValueString("drPracticeName");
String strDRPhone1 =
 doc.getItemValueString("drPhone1_N");
String strDRFax =
 doc.getItemValueString("drFax_N");
String strDRAdd1 =
 doc.getItemValueString("drAdd1");
String strDRAdd2 =
 doc.getItemValueString("drAdd2");
String strDRCity =
 doc.getItemValueString("drCity");
String strDRState1 =
 doc.getItemValueString("State1");
String strDRZip =
 doc.getItemValueString("drZip");
String strDRRepNo =
 doc.getItemValueString("RepNo");
String strDRUPIN =
 doc.getItemValueString("drUPIN");
String strDRFullName =
 doc.getItemValueString("drFName")
 + " " +  doc.getItemValueString("drLName");
String strDRSequence = "000";
String strDRCreateDate =
 doc.getItemValueString("createDate");
                              
String strTextToWrite = """ +
 strDRPracticeName + """ + "," + """ +
 strDRPhone1 + """ + "," + """ +
 strDRFax + """ + "," + """ +
 strDRAdd1 + """ + "," + """ + strDRAdd2
 + """ + "," + """ + strDRCity + """ + "," + """
 + strDRState1 + """ + "," + """ + strDRZip
 + """ + "," + """ + strDRRepNo + """ + ",
" + """ + strDRUPIN + """ + "," + """ +
 strDRFullName + """ + "," + """ +
 strDRSequence + """ + "," + """ +
 strDRCreateDate + """;

new PrintStream(fout).println (strTextToWrite);
                             
doc = view.getNextDocument(doc);
                            }              
    fout.close();
    sendMail("<Send To>", "", "", 
"Insurance DR Export Notice", 
"Insurance DR Export has been completed!!!!");
    
  } 
            catch(Exception e) 
                     {
sendMail("<Send To>", "", "", "Insurance 
DR Export Error Notice", "Insurance 
DR Export failed!!!!" + " " + e.getMessage());
           e.printStackTrace();
            }
 } 
 
public void sendMail(String strTo, 
String strCC, String strBCC, String 
strSubject, String strGetBody)
     {
          String mailBody;
          
      try {
Session curSession = getSession();
 AgentContext agentContext =
 curSession.getAgentContext();
Database curDb =
 agentContext.getCurrentDatabase();   
Document contextDoc =
 agentContext.getDocumentContext();                 
                
String mailSendTo = strTo;
String mailSubject = strSubject;        
                 
                 String mailBody1 
= "***********************************
*********************************************";
                 String mailBody2 =
 "<BR><BR>" + strGetBody;
                 String mailBody3 =
 "<BR><BR>" 
+ "***********************************
*********************************************";
                                  
mailBody = mailBody1 + " "
 + mailBody2 + " " + mailBody3;

Document mailDoc =
 curDb.createDocument();
mailDoc.replaceItemValue("Form", "Memo");
       
curSession.setConvertMIME(false); // Block conversion
Stream multiStream = curSession.createStream();
multiStream.writeText("Dit is een multipart bericht");
MIMEEntity mimeRoot = mailDoc.createMIMEEntity();
MIMEHeader header =
 mimeRoot.createHeader("Content-Transfer-Encoding");
                 
//mailinfo
header = mimeRoot.createHeader("Subject");
header.setHeaderVal(mailSubject);
header = mimeRoot.createHeader("To");
header.setHeaderVal(mailSendTo);
mimeRoot.setContentFromText(multiStream
,"multipart/related",MIMEEntity.ENC_NONE);
multiStream.close();  
   
//html toevoegen
Stream htmlStream = curSession.createStream();
mime = mimeRoot.createChildEntity();
htmlStream.writeText(mailBody);
mime.setContentFromText(htmlStream, 
"text/html;charset=ISO-8859-1", MIMEEntity.ENC_NONE);
htmlStream.close();
 
//email verzenden
mailDoc.send(false);
curSession.setConvertMIME(true); // Restore conversion
        } 
catch(Exception e) {
 e.printStackTrace();                 
                 }
     }

}

Do you have comments on this tip? Let us know.

This tip was submitted to the SearchDomino.com tip exchange by member Michael Marcavage. Please let others know how useful it is via the rating scale below. Do you have a useful Notes/Domino tip or code to share? Submit it to our bimonthly tip contest and you could win a prize and a spot in our Hall of Fame.

Rate this Tip
To rate tips, you must be a member of SearchDomino.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
Java
Java code shortens strings in a SQL table
How to execute a stored procedure in Lotus Notes Domino using Java
How to return an HTML representation of a Lotus Notes rich-text field
Shrink Lotus Notes databases with many attachments
Converting Lotus Notes Domino Web pages to PDF files with a Java agent
A bevy of Notes/Domino development tips
Converting Web pages to images using Java
Creating Microsoft Word documents from Lotus Notes
FAQ: Java for Lotus Notes and Domino
Automatically scan Lotus Notes database document attachments for viruses

Java for Lotus Notes Domino
Java code shortens strings in a SQL table
How to execute a stored procedure in Lotus Notes Domino using Java
Top 10 Lotus Notes Domino programming and development tips of 2007
How to return an HTML representation of a Lotus Notes rich-text field
Shrink Lotus Notes databases with many attachments
Converting Lotus Notes Domino Web pages to PDF files with a Java agent
Developing Eclipse plug-ins for Lotus Notes and Domino -- 7 tips in 7 minutes
A bevy of Notes/Domino development tips
Converting Web pages to images using Java
Creating Microsoft Word documents from Lotus Notes

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Domino & Lotus Notes Security Solutions: Authentication, Antispam, Encryption and Antivirus
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 1999 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts