Example usage for org.apache.commons.vfs2 Selectors SELECT_FILES

List of usage examples for org.apache.commons.vfs2 Selectors SELECT_FILES

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 Selectors SELECT_FILES.

Prototype

FileSelector SELECT_FILES

To view the source code for org.apache.commons.vfs2 Selectors SELECT_FILES.

Click Source Link

Document

A FileSelector that only files (not folders).

Usage

From source file:org.wso2.carbon.connector.FileCopy.java

/**
 * @param source      file location//from  w  w  w  . ja v  a 2 s  .  c o  m
 * @param destination target file location
 * @param filePattern pattern of the file
 * @param opts        FileSystemOptions
 * @throws IOException
 */
private void copy(String source, String destination, String filePattern, FileSystemOptions opts)
        throws IOException {
    StandardFileSystemManager manager = FileConnectorUtils.getManager();
    FileObject souFile = manager.resolveFile(source, opts);
    FileObject[] children = souFile.getChildren();
    FilePattenMatcher patternMatcher = new FilePattenMatcher(filePattern);
    for (FileObject child : children) {
        try {
            if (patternMatcher.validate(child.getName().getBaseName())) {
                String name = child.getName().getBaseName();
                FileObject outFile = manager.resolveFile(destination + File.separator + name, opts);
                outFile.copyFrom(child, Selectors.SELECT_FILES);
            }
        } catch (IOException e) {
            log.error("Error occurred while copying a file. " + e.getMessage(), e);
        }
    }
    manager.close();
}

From source file:tain.kr.test.vfs.v01.TestVfs2FilehandleService.java

@Test
public void testCreateFile() throws Exception {

    FileSystemManager manager = VFS.getManager();

    FileObject baseDir = manager.resolveFile(this.absoluteFilePath);
    final FileObject file = manager.resolveFile(baseDir, "testfolder/file1.txt");

    // delete a file
    file.delete(Selectors.SELECT_FILES);
    assertFalse(file.exists());/*from  www. ja  v a2 s. c  o  m*/

    // create a file
    file.createFile();
    assertTrue(file.exists());
}

From source file:tain.kr.test.vfs.v01.TestVfs2FilehandleService.java

@Test
public void testAccessFile() throws Exception {

    FileSystemManager manager = VFS.getManager();

    FileObject baseDir = manager.resolveFile(this.absoluteFilePath);
    FileObject file = manager.resolveFile(baseDir, "testfolder/file1.txt");

    //   //w w w  .j  a v a 2  s.c o  m
    file.delete(Selectors.SELECT_FILES);
    assertFalse(file.exists());

    //  
    file.createFile();
    assertTrue(file.exists());

    FileContent fileContent = file.getContent();
    assertEquals(0, fileContent.getSize());

    //  
    String string = "test.";
    OutputStream os = fileContent.getOutputStream();

    try {
        os.write(string.getBytes());
        os.flush();
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (Exception ignore) {
                // no-op
            }
        }
    }
    assertNotSame(0, fileContent.getSize());

    //  
    StringBuffer sb = new StringBuffer();
    FileObject writtenFile = manager.resolveFile(baseDir, "testfolder/file1.txt");
    FileContent writtenContents = writtenFile.getContent();
    InputStream is = writtenContents.getInputStream();

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        String line = "";
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ignore) {
                // no-op
            }
        }
    }

    //  
    assertEquals(sb.toString(), string);
}