ShowTable of Contents
Bytes, close
This button opens, collects information, and closes the files in a directory.
var filepath:string = requestScope.filepath;
var stream:NotesStream = session.createStream();
var bytes:int = 0;
var files = (new java.io.File(filepath)).listFiles();
for (var i = 0; i < files.length; i++) {
requestScope.status += "\n" + files[i];
if (files[i].isFile()) {
try {
if (stream.open(files[i].getPath())) {
bytes += stream.getBytes();
requestScope.status += ", " + stream.getBytes() + " bytes";
stream.close();
} else {
requestScope.status += ", cannot open";
}
} catch(e) {
requestScope.status += ", cannot open";
}
}
}
requestScope.status += "\n" + "Number of files = " + files.length;
requestScope.status += "\n" + "Total bytes = " + bytes;
Charset
This button uses Turkish (ISO-8859-4) characters to create a document. To correctly display the document, your machine must be configured to display Unicode in Notes.
var inPath:string = "c:\\turkish.txt";
var charset:string = "ISO-8859-4";
session.setConvertMIME(false);
var inStream:NotesStream = session.createStream();
if (inStream.open(inPath, charset)) {
if (inStream.getBytes() > 0) {
var doc:NotesDocument = database.createDocument();
doc.replaceItemValue("Form", "main");
doc.replaceItemValue("subject", inPath);
var mime:NotesMIMEEntity = doc.createMIMEEntity();
if (mime != null) {
mime.setContentFromText(inStream,
"text/plain; charset=" + charset, NotesMIMEEntity.ENC_NONE);
mime.encodeContent(NotesMIMEEntity.ENC_QUOTED_PRINTABLE);
requestScope.status = "Charset = " + mime.getCharset();
}
doc.save(true, true);
} else requstScope.status = "Input file has no content";
inStream.close();
} else requstScope.status = "Input file open failed";
session.setConvertMIME(true);
IsEOS, read, write
This button makes an exact copy of a file.
var inPath:string = requestScope.filepath;
var n:int = inPath.lastIndexOf(".");
var outPath:string = inPath.left(n) + "Copy" + inPath.right(inPath.length - n);
var inStream:NotesStream = session.createStream();
if (inStream.open(inPath, "binary")) {
if (inStream.getBytes() > 0) {
var outStream:NotesStream = session.createStream();
if (outStream.open(outPath, "binary")) {
if (!outStream.isReadOnly()) {
do {
var buffer = inStream.read(32767);
outStream.write(buffer);
} while (!inStream.isEOS());
} else requestScope.status = "Output file exists and is read-only";
outStream.close();
} else requestScope.status = "Output file open failed";
} else requestScope.status = "Input file has no content";
inStream.close();
} else requestScope.status = "Input file open failed";
IsReadOnly, writeText
This button writes the subject and body items of the current document to a text file. The button first ensures that the file does not exist as a read-only file.
var filepath:string = database.getFileName();
filepath = "c:\\" + filepath.left(filepath.length - 3) + "txt";
requestScope.status = "Output file: " + filepath;
var stream:NotesStream = session.createStream();
if (stream.open(filepath, "ASCII")) {
if (stream.isReadOnly()) {
requestScope.status = filepath + " is read-only";
return;
}
stream.truncate();
requestScope.status += "\nBytes written: " +
stream.writeText(currentDocument.getItemValueString("subject"),
NotesStream.EOL_CRLF);
if (document1.hasItem("body")) {
requestScope.status += "\nBytes written: " +
stream.writeText(document1.getItemValue("body").firstElement());
} else {
requestScope.status += "\nBytes written: 0";
}
stream.close();
} else {
requestScope.status = "Output file open failed";
}
Position
This button writes the content of an item to a stream, then positions the stream to the beginning (0) before reading from it.
var doc:NotesDocument = currentDocument.getDocument();
// Create stream and write text of Body item to it
var stream:NotesStream = session.createStream();
stream.writeText(doc.getItemValueString("body"));
// Reset position to beginning and read text
stream.setPosition(0);
requestScope.status = stream.readText();
getContents
This button creates a file from the subject and body content of a document.
var doc:NotesDocument = currentDocument.getDocument();
var outPath:string = "c:\\" + doc.getItemValueString("subject") + ".txt";
var fos = new java.io.FileOutputStream(outPath);
var osw = new java.io.OutputStreamWriter(fos);
var outStream:NotesStream = session.createStream();
outStream.writeText(doc.getItemValueString("body"));
outStream.getContents(osw);
osw.close();
outStream.close();
open, truncate, writeText
This button creates a note collection from the documents in the current database and exports it to a text file as DXL.
var stream:NotesStream = session.createStream();
var filename:string = "c:\\dxl\\";
filename = filename + database.getFileName();
filename = filename.substring(0, filename.length() - 3) + "dxl";
if (stream.open(filename)) {
requestScope.status = "Opened " + filename;
stream.truncate();
// Create note collection
var nc:NotesNoteCollection = database.createNoteCollection(false);
nc.setSelectDocuments(true);
nc.buildCollection();
// Export note collection as DXL
var exporter:NotesDxlExporter = session.createDxlExporter();
var output:string = exporter.exportDxl(nc);
stream.writeText(output);
requestScope.status = "Exported note collection as DXL ";
stream.close();
} else {
requestScope.status = "Unable to open " + filename;
}
readText
This button creates a document whose body contains the content of a file.
var inPath:string = requestScope.filepath;
var inStream:NotesStream = session.createStream();
if (inStream.open(inPath, "ASCII")) {
if (inStream.getBytes() > 0) {
var doc = database.createDocument();
doc.replaceItemValue("Form", "main");
doc.replaceItemValue("subject", inPath);
doc.replaceItemValue("body", inStream.readText());
doc.save(true, true);
} else requestScope.status = "Input file has no content";
inStream.close();
} else requestScope.status = "Input file open failed";
This button is the same except that it processes the file a line at a time.
var inPath:string = requestScope.filepath;
var inStream:NotesStream = session.createStream();
if (inStream.open(inPath, "ASCII")) {
if (inStream.getBytes() > 0) {
var doc = database.createDocument();
doc.replaceItemValue("Form", "main");
doc.replaceItemValue("subject", inPath);
var body:NotesRichTextItem = doc.createRichTextItem("body");
do {
body.appendText(inStream.readText(NotesStream.STMREAD_LINE,
NotesStream.EOL_CRLF));
} while (!inStream.isEOS());
doc.save(true, true);
} else requestScope.status = "Input file has no content";
inStream.close();
} else requestScope.status = "Input file open failed";
setContents
This button creates a document from the name and content of a file.
var inPath:string = requestScope.filename;
var fis = new java.io.FileInputStream(inPath);
var isr = new java.io.InputStreamReader(fis, "ASCII");
var inStream:NotesStream = session.createStream();
inStream.setContents(isr);
var doc:NotesDocument = database.createDocument();
doc.replaceItemValue("Form", "main");
doc.replaceItemValue("subject", inPath);
doc.replaceItemValue("body", inStream.readText());
inStream.close();
doc.save(true, true);