Example usage for org.apache.commons.vfs2 FileObject exists

List of usage examples for org.apache.commons.vfs2 FileObject exists

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject exists.

Prototype

boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

From source file:org.apache.accumulo.start.classloader.vfs.providers.ReadOnlyHdfsFileProviderTest.java

@Test
public void testGetAttributes() throws Exception {
    FileObject fo = manager.resolveFile(TEST_DIR1);
    Assert.assertNotNull(fo);/*from   ww  w .j av a 2s  .co m*/
    Assert.assertFalse(fo.exists());

    // Create the test file
    FileObject file = createTestFile(hdfs);
    Map<String, Object> attributes = file.getContent().getAttributes();
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.BLOCK_SIZE.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.GROUP.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.LAST_ACCESS_TIME.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.LENGTH.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.MODIFICATION_TIME.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.OWNER.toString()));
    Assert.assertTrue(attributes.containsKey(HdfsFileAttributes.PERMISSIONS.toString()));
}

From source file:org.apache.accumulo.start.classloader.vfs.providers.ReadOnlyHdfsFileProviderTest.java

@Test(expected = FileSystemException.class)
public void testRandomAccessContent() throws Exception {
    FileObject fo = manager.resolveFile(TEST_DIR1);
    Assert.assertNotNull(fo);/*from  w w w  .j  av a2  s .  c o  m*/
    Assert.assertFalse(fo.exists());

    // Create the test file
    FileObject file = createTestFile(hdfs);
    file.getContent().getRandomAccessContent(RandomAccessMode.READWRITE).close();
}

From source file:org.apache.accumulo.start.classloader.vfs.providers.ReadOnlyHdfsFileProviderTest.java

@Test
public void testRandomAccessContent2() throws Exception {
    FileObject fo = manager.resolveFile(TEST_DIR1);
    Assert.assertNotNull(fo);//from   w w  w .  jav a2s .c  om
    Assert.assertFalse(fo.exists());

    // Create the test file
    FileObject file = createTestFile(hdfs);
    file.getContent().getRandomAccessContent(RandomAccessMode.READ).close();
}

From source file:org.apache.accumulo.start.classloader.vfs.providers.ReadOnlyHdfsFileProviderTest.java

@Test
public void testEquals() throws Exception {
    FileObject fo = manager.resolveFile(TEST_DIR1);
    Assert.assertNotNull(fo);/*from  w w w  .  j  a  v  a  2 s  .c o  m*/
    Assert.assertFalse(fo.exists());

    // Create the test file
    FileObject file = createTestFile(hdfs);
    // Get a handle to the same file
    FileObject file2 = manager.resolveFile(TEST_FILE1);
    Assert.assertEquals(file, file2);
}

From source file:org.apache.commons.vfs2.example.Shell.java

/**
 * Does a 'cp' command.//ww w  .j av a 2s  . c o m
 */
private void cp(final String[] cmd) throws Exception {
    if (cmd.length < 3) {
        throw new Exception("USAGE: cp <src> <dest>");
    }

    final FileObject src = mgr.resolveFile(cwd, cmd[1]);
    FileObject dest = mgr.resolveFile(cwd, cmd[2]);
    if (dest.exists() && dest.getType() == FileType.FOLDER) {
        dest = dest.resolveFile(src.getName().getBaseName());
    }

    dest.copyFrom(src, Selectors.SELECT_ALL);
}

From source file:org.apache.commons.vfs2.example.Shell.java

/**
 * Does a 'cd' command./*  w ww  . java  2s. co m*/
 * If the taget directory does not exist, a message is printed to <code>System.err</code>.
 */
private void cd(final String[] cmd) throws Exception {
    final String path;
    if (cmd.length > 1) {
        path = cmd[1];
    } else {
        path = System.getProperty("user.home");
    }

    // Locate and validate the folder
    final FileObject tmp = mgr.resolveFile(cwd, path);
    if (tmp.exists()) {
        cwd = tmp;
    } else {
        System.out.println("Folder does not exist: " + tmp.getName());
    }
    System.out.println("Current folder is " + cwd.getName());
}

From source file:org.apache.commons.vfs2.example.Shell.java

/**
 * Does a 'touch' command./*from ww  w .  j  a  v a2s . co  m*/
 */
private void touch(final String[] cmd) throws Exception {
    if (cmd.length < 2) {
        throw new Exception("USAGE: touch <path>");
    }
    final FileObject file = mgr.resolveFile(cwd, cmd[1]);
    if (!file.exists()) {
        file.createFile();
    }
    file.getContent().setLastModifiedTime(System.currentTimeMillis());
}

From source file:org.apache.commons.vfs2.example.ShowProperties.java

public static void main(final String[] args) {
    if (args.length == 0) {
        System.err.println("Please pass the name of a file as parameter.");
        System.err.println("e.g. java org.apache.commons.vfs2.example.ShowProperties LICENSE.txt");
        return;/*from www  . j a v  a2 s.  c  o  m*/
    }
    for (final String arg : args) {
        try {
            final FileSystemManager mgr = VFS.getManager();
            System.out.println();
            System.out.println("Parsing: " + arg);
            final FileObject file = mgr.resolveFile(arg);
            System.out.println("URL: " + file.getURL());
            System.out.println("getName(): " + file.getName());
            System.out.println("BaseName: " + file.getName().getBaseName());
            System.out.println("Extension: " + file.getName().getExtension());
            System.out.println("Path: " + file.getName().getPath());
            System.out.println("Scheme: " + file.getName().getScheme());
            System.out.println("URI: " + file.getName().getURI());
            System.out.println("Root URI: " + file.getName().getRootURI());
            System.out.println("Parent: " + file.getName().getParent());
            System.out.println("Type: " + file.getType());
            System.out.println("Exists: " + file.exists());
            System.out.println("Readable: " + file.isReadable());
            System.out.println("Writeable: " + file.isWriteable());
            System.out.println("Root path: " + file.getFileSystem().getRoot().getName().getPath());
            if (file.exists()) {
                if (file.getType().equals(FileType.FILE)) {
                    System.out.println("Size: " + file.getContent().getSize() + " bytes");
                } else if (file.getType().equals(FileType.FOLDER) && file.isReadable()) {
                    final FileObject[] children = file.getChildren();
                    System.out.println("Directory with " + children.length + " files");
                    for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
                        System.out.println("#" + iterChildren + ": " + children[iterChildren].getName());
                        if (iterChildren > SHOW_MAX) {
                            break;
                        }
                    }
                }
                System.out.println("Last modified: "
                        + DateFormat.getInstance().format(new Date(file.getContent().getLastModifiedTime())));
            } else {
                System.out.println("The file does not exist");
            }
            file.close();
        } catch (final FileSystemException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:org.apache.hadoop.gateway.topology.file.FileTopologyProvider.java

private Map<FileName, Topology> loadTopologies(FileObject directory) throws FileSystemException {
    Map<FileName, Topology> map = new HashMap<FileName, Topology>();
    if (directory.exists() && directory.getType().hasChildren()) {
        for (FileObject file : directory.getChildren()) {
            if (file.exists() && !file.getType().hasChildren()
                    && SUPPORTED_TOPOLOGY_FILE_EXTENSIONS.contains(file.getName().getExtension())) {
                try {
                    map.put(file.getName(), loadTopology(file));
                } catch (IOException e) {
                    // Maybe it makes sense to throw exception
                    log.failedToLoadTopology(file.getName().getFriendlyURI(), e);
                } catch (SAXException e) {
                    // Maybe it makes sense to throw exception
                    log.failedToLoadTopology(file.getName().getFriendlyURI(), e);
                } catch (Exception e) {
                    // Maybe it makes sense to throw exception
                    log.failedToLoadTopology(file.getName().getFriendlyURI(), e);
                }// w  w  w .  ja v  a  2  s  .c  om
            }
        }
    }
    return map;
}

From source file:org.apache.hadoop.gateway.topology.file.FileTopologyProviderTest.java

private FileObject createDir(String name) throws FileSystemException {
    FileSystemManager fsm = VFS.getManager();
    FileObject dir = fsm.resolveFile(name);
    dir.createFolder();//from   ww  w . ja va 2  s .  co  m
    assertTrue("Failed to create test dir " + dir.getName().getFriendlyURI(), dir.exists());
    return dir;
}