JAVA/CORBA CLASSES
Examples: open method
1. This example gets information on the normal files in a directory by opening each file as a stream. The agent reuses the same NotesStream object by opening and closing it for each file.
import lotus.domino.*;
import java.io.File;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
// (Your code goes here)
Stream stream = session.createStream();
int bytes = 0;
File directory = new File("C:\\StreamFiles");
String[] files = directory.list();
for (int i = 0; i < files.length; i++) {
if (stream.open(directory + "\\" + files[i])) {
bytes = bytes + stream.getBytes();
stream.close();
}
else
throw new NotesException
(NotesError.NOTES_ERR_FILEOPEN_FAILED, "Open failed");
}
System.out.println("Number of files = " + files.length);
System.out.println("Total bytes = " + bytes);
} catch(NotesException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
2. This agent opens a file as a stream for reading.
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();
}
}
}
3. This agent opens a file for writing.
import lotus.domino.*;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
// (Your code goes here)
// Get the input file
DocumentCollection dc = agentContext.getUnprocessedDocuments();
Document doc = dc.getFirstDocument();
String outPath =
"c:\\StreamFiles\\" + doc.getItemValueString("Subject") + ".txt";
Stream outStream = session.createStream();
if (outStream.open(outPath, "ASCII")) {
if (outStream.getBytes() == 0) {
outStream.writeText(doc.getItemValueString("Body"));
outStream.close();
}
else
System.out.println("Output file exists and has content");
}
else
System.out.println("Output file open failed");
} catch(NotesException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
See Also
open method
Glossary
Feedback on
Help
or
Product Usability
?
Help on Help
All Help Contents
Glossary