Examples: readText method

  1. This agent reads a text file and saves its content in the Body item of a new document in the current database. The agent uses the file name for the content of the Subject item.
    import lotus.domino.*;
    
    public class JavaAgent extends AgentBase {
    
      public void NotesMain() {
    
        try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
    
          // (Your code goes here)
          String inPath = "c:\\StreamFiles\\readme.txt";
          
          // Get the input file
          Stream inStream = session.createStream();
          if (inStream.open(inPath, "ASCII")) {
            if (inStream.getBytes() > 0) {
              Database db = agentContext.getCurrentDatabase();
              Document doc = db.createDocument();
              doc.replaceItemValue("Form", "Main Topic");
              doc.replaceItemValue("Subject", inPath);
              doc.replaceItemValue("Body", inStream.readText());
              inStream.close();
              doc.save(true, true);
            }
            else
              System.out.println("Input file has no content");
          }
          else
            System.out.println("Input file open failed");
          
        } catch(NotesException e) {
          e.printStackTrace();
    
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  2. This agent is similar to the first example but reads the file a line at a time. The agent adds text to the Body item of the new document until end of stream occurs.
    import lotus.domino.*;
    
    public class JavaAgent extends AgentBase {
    
      public void NotesMain() {
    
        try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
    
          // (Your code goes here)
          String inPath = "c:\\StreamFiles\\readme.txt";
          
          // Get the input file
          Stream inStream = session.createStream();
          if (inStream.open(inPath, "ASCII")) {
            if (inStream.getBytes() > 0) {
              Database db = agentContext.getCurrentDatabase();
              Document doc = db.createDocument();
              doc.replaceItemValue("Form", "Main Topic");
              doc.replaceItemValue("Subject", inPath);
              RichTextItem body = doc.createRichTextItem("Body");
              do {
                body.appendText(inStream.readText(
                  Stream.STMREAD_LINE,
                  Stream.EOL_CRLF));
              } while (!inStream.isEOS());
              inStream.close();
              doc.save(true, true);
            }
            else
              System.out.println("Input file has no content");
          }
          else
            System.out.println("Input file open failed");
          
        } catch(NotesException e) {
          e.printStackTrace();
    
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }