本文整理汇总了Java中org.openide.filesystems.FileObject.createAndOpen方法的典型用法代码示例。如果您正苦于以下问题:Java FileObject.createAndOpen方法的具体用法?Java FileObject.createAndOpen怎么用?Java FileObject.createAndOpen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.openide.filesystems.FileObject
的用法示例。
在下文中一共展示了FileObject.createAndOpen方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createAndCheckTextContent
import org.openide.filesystems.FileObject; //导入方法依赖的package包/类
private void createAndCheckTextContent(String encoding) {
FileObject root = FileUtil.createMemoryFileSystem().getRoot();
try {
OutputStream os = root.createAndOpen("file");
try {
OutputStreamWriter osw = new OutputStreamWriter(os,
encoding);
try {
osw.append("Test Text");
osw.flush();
} finally {
osw.close();
}
} finally {
os.close();
}
assertTrue("File with encoding " + encoding
+ " was detected as binary file",
DefaultMatcher.hasTextContent(root.getFileObject("file")));
} catch (UnsupportedEncodingException eee) {
LOG.log(Level.INFO, "Unknown encoding {0}", encoding);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
示例2: writeFile
import org.openide.filesystems.FileObject; //导入方法依赖的package包/类
/**
* Create a new data file with specified initial contents.
* No file events should be fired until the resulting file is complete (see {@link FileObject#createAndOpen}).
* @param root a root folder which should already exist
* @param path a /-separated path to the new file within that root
* @param body the complete contents of the new file (in UTF-8 encoding)
*/
public static FileObject writeFile(FileObject root, String path, String body) throws IOException {
int slash = path.lastIndexOf('/');
if (slash != -1) {
root = FileUtil.createFolder(root, path.substring(0, slash));
path = path.substring(slash + 1);
}
FileObject existing = root.getFileObject(path);
OutputStream os = existing != null ? existing.getOutputStream() : root.createAndOpen(path);
try {
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
pw.print(body);
pw.flush();
} finally {
os.close();
}
return root.getFileObject(path);
}
示例3: writeZipFile
import org.openide.filesystems.FileObject; //导入方法依赖的package包/类
/**
* Create a new ZIP file.
* No file events should be fired until the resulting file is complete (see {@link FileObject#createAndOpen}).
* @param root a root folder which should already exist
* @param path a /-separated path to the new ZIP file within that root
* @param entries a list of entries in the form of "filename:UTF8-contents"; parent dirs created automatically
* @return the newly created ZIP file (use {@link FileUtil#getArchiveRoot} if you want the root entry)
* @throws IOException for the usual reasons
*/
public static FileObject writeZipFile(FileObject root, String path, String... entries) throws IOException {
int slash = path.lastIndexOf('/');
if (slash != -1) {
root = FileUtil.createFolder(root, path.substring(0, slash));
path = path.substring(slash + 1);
}
FileObject existing = root.getFileObject(path);
OutputStream os = existing != null ? existing.getOutputStream() : root.createAndOpen(path);
try {
org.openide.util.test.TestFileUtils.writeZipFile(os, entries);
} finally {
os.close();
}
return root.getFileObject(path);
}
示例4: createZipFile
import org.openide.filesystems.FileObject; //导入方法依赖的package包/类
/**
* Creates ZIP file archive.zip that contains three files, a.txt, b.txt and
* c.txt.
*/
private void createZipFile(FileObject parentFolder) throws IOException {
OutputStream outStream = parentFolder.createAndOpen("archive.zip");
ZipOutputStream zipStream = new ZipOutputStream(outStream);
zipStream.putNextEntry(new ZipEntry("a.txt"));
zipStream.write(new byte[]{1, 2, 4, 5, 6, 7, 8});
zipStream.closeEntry();
zipStream.putNextEntry(new ZipEntry("b.txt"));
zipStream.write(new byte[]{1, 2, 4, 5, 6, 7, 8});
zipStream.closeEntry();
zipStream.putNextEntry(new ZipEntry("c.txt"));
zipStream.write(new byte[]{1, 2, 4, 5, 6, 7, 8});
zipStream.closeEntry();
zipStream.close();
outStream.close();
}
示例5: testHasTextContentWithBinaryContent
import org.openide.filesystems.FileObject; //导入方法依赖的package包/类
public void testHasTextContentWithBinaryContent() {
FileObject root = FileUtil.createMemoryFileSystem().getRoot();
try {
OutputStream os = root.createAndOpen("file");
try {
os.write(new byte[]{90, 98, 88, 97, 94, 0, 1, 2, 4, 5, 4, 6});
} finally {
os.close();
}
assertFalse("Binary file was detected as textual file",
DefaultMatcher.hasTextContent(root.getFileObject("file")));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
示例6: writeSourceFile
import org.openide.filesystems.FileObject; //导入方法依赖的package包/类
private void writeSourceFile() throws Exception {
File f = new File(getWorkDir(), "source.fxml");
FileObject dirFo = FileUtil.toFileObject(getWorkDir());
OutputStream ostm = dirFo.createAndOpen("source.fxml");
OutputStreamWriter wr = new OutputStreamWriter(ostm);
wr.write(text);
wr.flush();
wr.close();
sourceFO = dirFo.getFileObject("source.fxml");
}
示例7: testRegularURI
import org.openide.filesystems.FileObject; //导入方法依赖的package包/类
/**
* Checks that file with just regular characters in name is translated OK
* @throws Exception
*/
public void testRegularURI() throws Exception {
FileObject fo = FileUtil.getConfigRoot();
FileObject origDir = fo.createFolder("origFolder");
FileObject newFile = origDir.createData("regularFileName.txt");
final FileObject d = fo.createFolder("subfolder");
OutputStream ostm = d.createAndOpen("regularShadowURI.shadow");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ostm));
URI uri = newFile.toURI();
String urlString = newFile.toURI().toString();
bw.write(urlString + ".old");
bw.newLine();
bw.newLine();
bw.close();
FileObject fob = d.getFileObject("regularShadowURI.shadow");
DataObject dd = DataObject.find(fob);
assertTrue("Shadow must be translated, not broken", dd instanceof DataShadow);
DataShadow ds = (DataShadow)dd;
assertEquals("Shadow's original must be on the translated location", newFile, ds.getOriginal().getPrimaryFile());
}
示例8: testFSNameAndPath
import org.openide.filesystems.FileObject; //导入方法依赖的package包/类
/**
* Checks translation on Shadows, which use FS name + path, not URI
* @throws Exception
*/
public void testFSNameAndPath() throws Exception {
FileObject fo = FileUtil.getConfigRoot();
FileObject origDir = fo.createFolder("origFolder3");
// create empty real file with special and non-ASCII chars
FileObject newFile = origDir.createData("moved-here.txt");
// createa a fake file, just to get its URI right:
FileObject fake = fo.createData("dead-file-location.old");
final FileObject d = fo.createFolder("subfolder3");
OutputStream ostm = d.createAndOpen("regularShadowURI.shadow");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ostm));
bw.write(fake.getPath());
bw.newLine();
bw.write(fake.getFileSystem().getSystemName());
bw.newLine();
fake.delete();
bw.close();
FileObject fob = d.getFileObject("regularShadowURI.shadow");
DataObject dd = DataObject.find(fob);
assertTrue("Shadow must be translated, not broken", dd instanceof DataShadow);
DataShadow ds = (DataShadow)dd;
assertEquals("Shadow's original must be on the translated location", newFile, ds.getOriginal().getPrimaryFile());
}
示例9: testNonSFSUriNotAffected
import org.openide.filesystems.FileObject; //导入方法依赖的package包/类
/**
* Checks that DataShadows to regular (non-SFS) files are not translated
* even if a translation is defined for their path
*
* @throws Exception
*/
public void testNonSFSUriNotAffected() throws Exception {
File wd = getWorkDir();
clearWorkDir();
FileObject origDir = FileUtil.toFileObject(wd);
FileObject dirWithSpace = origDir.createFolder("Space Dir");
FileObject newFile = dirWithSpace.createData("testFile.txt");
File subDir = new File(wd, "translate");
subDir.mkdirs();
File metaTranslate = new File(subDir, "META-INF/netbeans");
metaTranslate.mkdirs();
String workPath = newFile.toURI().getRawPath();
FileWriter wr = new FileWriter(new File(metaTranslate, "translate.names"));
BufferedWriter bw = new BufferedWriter(wr);
bw.write(workPath.substring(1) + "/testFile.txt=" + workPath.substring(1) + "/moved/testFile.txt");
bw.close();
FileObject fo = FileUtil.toFileObject(wd);
ClassLoader orig = Lookup.getDefault().lookup(ClassLoader.class);
ClassLoader my = new URLClassLoader(new URL[] {
subDir.toURL()
}, orig);
MockLookup.setInstances(my);
FileObject cfgRoot = FileUtil.getConfigRoot();
OutputStream ostm = cfgRoot.createAndOpen("nonSFSFile.shadow");
bw = new BufferedWriter(new OutputStreamWriter(ostm));
bw.write(newFile.toURI().toString());
bw.newLine();
bw.newLine();
newFile.delete();
bw.close();
FileObject fob = cfgRoot.getFileObject("nonSFSFile.shadow");
DataObject dd = DataObject.find(fob);
assertFalse("Shadow must be still broken", dd instanceof DataShadow);
}