Tip

Java code to write to text file

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.

This was first published in October 2005

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

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.