📄 writeauthorandtitle.java
字号:
t = ex; } catch (WritingNotSupportedException ex) { t = ex; } /* According to the definition of the processPOIFSReaderEvent method * we cannot pass checked exceptions to the caller. The following * lines check whether a checked exception occured and throws an * unchecked exception. The message of that exception is that of * the underlying checked exception. */ if (t != null) { throw new HPSFRuntimeException ("Could not read file \"" + path + "/" + name + "\". Reason: " + Util.toString(t)); } } /** * <p>Receives a summary information property set modifies (or creates) * its "author" and "title" properties and writes the result under the * same path and name as the origin to a destination POI filesystem.</p> * * @param poiFs The POI filesystem to write to. * @param path The original (and destination) stream's path. * @param name The original (and destination) stream's name. * @param si The property set. It should be a summary information * property set. * @throws IOException * @throws WritingNotSupportedException */ public void editSI(final POIFSFileSystem poiFs, final POIFSDocumentPath path, final String name, final PropertySet si) throws WritingNotSupportedException, IOException { /* Get the directory entry for the target stream. */ final DirectoryEntry de = getPath(poiFs, path); /* Create a mutable property set as a copy of the original read-only * property set. */ final MutablePropertySet mps = new MutablePropertySet(si); /* Retrieve the section containing the properties to modify. A * summary information property set contains exactly one section. */ final MutableSection s = (MutableSection) mps.getSections().get(0); /* Set the properties. */ s.setProperty(PropertyIDMap.PID_AUTHOR, Variant.VT_LPSTR, "Rainer Klute"); s.setProperty(PropertyIDMap.PID_TITLE, Variant.VT_LPWSTR, "Test"); /* Create an input stream containing the bytes the property set * stream consists of. */ final InputStream pss = mps.toInputStream(); /* Write the property set stream to the POIFS. */ de.createDocument(name, pss); } /** * <p>Writes a {@link PropertySet} to a POI filesystem. This method is * simpler than {@link #editSI} because the origin property set has just * to be copied.</p> * * @param poiFs The POI filesystem to write to. * @param path The file's path in the POI filesystem. * @param name The file's name in the POI filesystem. * @param ps The property set to write. * @throws WritingNotSupportedException * @throws IOException */ public void copy(final POIFSFileSystem poiFs, final POIFSDocumentPath path, final String name, final PropertySet ps) throws WritingNotSupportedException, IOException { final DirectoryEntry de = getPath(poiFs, path); final MutablePropertySet mps = new MutablePropertySet(ps); de.createDocument(name, mps.toInputStream()); } /** * <p>Copies the bytes from a {@link DocumentInputStream} to a new * stream in a POI filesystem.</p> * * @param poiFs The POI filesystem to write to. * @param path The source document's path. * @param name The source document's name. * @param stream The stream containing the source document. * @throws IOException */ public void copy(final POIFSFileSystem poiFs, final POIFSDocumentPath path, final String name, final DocumentInputStream stream) throws IOException { final DirectoryEntry de = getPath(poiFs, path); final ByteArrayOutputStream out = new ByteArrayOutputStream(); int c; while ((c = stream.read()) != -1) out.write(c); stream.close(); out.close(); final InputStream in = new ByteArrayInputStream(out.toByteArray()); de.createDocument(name, in); } /** * <p>Writes the POI file system to a disk file.</p> * * @throws FileNotFoundException * @throws IOException */ public void close() throws FileNotFoundException, IOException { out = new FileOutputStream(dstName); poiFs.writeFilesystem(out); out.close(); } /** Contains the directory paths that have already been created in the * output POI filesystem and maps them to their corresponding * {@link org.apache.poi.poifs.filesystem.DirectoryNode}s. */ private final Map paths = new HashMap(); /** * <p>Ensures that the directory hierarchy for a document in a POI * fileystem is in place. When a document is to be created somewhere in * a POI filesystem its directory must be created first. This method * creates all directories between the POI filesystem root and the * directory the document should belong to which do not yet exist.</p> * * <p>Unfortunately POI does not offer a simple method to interrogate * the POIFS whether a certain child node (file or directory) exists in * a directory. However, since we always start with an empty POIFS which * contains the root directory only and since each directory in the * POIFS is created by this method we can maintain the POIFS's directory * hierarchy ourselves: The {@link DirectoryEntry} of each directory * created is stored in a {@link Map}. The directories' path names map * to the corresponding {@link DirectoryEntry} instances.</p> * * @param poiFs The POI filesystem the directory hierarchy is created * in, if needed. * @param path The document's path. This method creates those directory * components of this hierarchy which do not yet exist. * @return The directory entry of the document path's parent. The caller * should use this {@link DirectoryEntry} to create documents in it. */ public DirectoryEntry getPath(final POIFSFileSystem poiFs, final POIFSDocumentPath path) { try { /* Check whether this directory has already been created. */ final String s = path.toString(); DirectoryEntry de = (DirectoryEntry) paths.get(s); if (de != null) /* Yes: return the corresponding DirectoryEntry. */ return de; /* No: We have to create the directory - or return the root's * DirectoryEntry. */ int l = path.length(); if (l == 0) /* Get the root directory. It does not have to be created * since it always exists in a POIFS. */ de = poiFs.getRoot(); else { /* Create a subordinate directory. The first step is to * ensure that the parent directory exists: */ de = getPath(poiFs, path.getParent()); /* Now create the target directory: */ de = de.createDirectory(path.getComponent (path.length() - 1)); } paths.put(s, de); return de; } catch (IOException ex) { /* This exception will be thrown if the directory already * exists. However, since we have full control about directory * creation we can ensure that this will never happen. */ ex.printStackTrace(System.err); throw new RuntimeException(ex.toString()); /* FIXME (2): Replace the previous line by the following once we * no longer need JDK 1.3 compatibility. */ // throw new RuntimeException(ex); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -