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.

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

Dig deeper on Java for Lotus Notes Domino

0 comments

Oldest 

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:

SearchWinIT

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchVirtualDataCentre.co.uk

Close