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:sftpexamples.SftpUtility.java

/**
 * Method to check if the remote file exists in the specified remote
 * location/*from  ww  w  .  ja va 2  s . com*/
 *
 * @param hostName HostName of the server
 * @param username UserName to login
 * @param password Password to login
 * @param remoteFilePath remoteFilePath. Should contain the entire remote
 * file path - Directory and Filename with / as separator
 * @return Returns if the file exists in the specified remote location
 */
public static boolean exist(String hostName, String username, String password, String remoteFilePath) {
    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {
        manager.init();

        // Create remote object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions());

        System.out.println("File exist: " + remoteFile.exists());

        return remoteFile.exists();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        manager.close();
    }
}

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

/**
 * Does a 'cp' command.//from   w  w  w  .j a  v a2s .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:tain.kr.test.vfs.v01.Shell.java

/**
 * Does a 'cd' command./*from  ww w  . j  ava  2  s.com*/
 * 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:tain.kr.test.vfs.v01.Shell.java

/**
 * Does a 'touch' command.//from   w  w w . j  a  v  a 2s  . c o  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:tain.kr.test.vfs.v01.ShowProperties.java

private static void test01(String[] args) throws Exception {

    if (flag)/*from  w w  w  .  ja v  a 2s .  c  o m*/
        new ShowProperties();

    if (flag) {

        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;
        }

        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: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());

    // create a file
    file.createFile();/*  w  ww  .ja  v a 2  s.  c o m*/
    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");

    //   /*from ww  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);
}

From source file:tw.edu.sinica.iis.SSHadoop.SSHSftp.java

public void sftpDelete(final String remoteDir) {
    try {//from w w  w .j a  v  a 2  s.c o m
        manager.init();

        FileObject remote = manager.resolveFile(genConnectString() + remoteDir, opts.getOptions());

        if (remote.exists()) {
            remote.delete(Selectors.SELECT_ALL);
        }

    } catch (FileSystemException e) {
        e.printStackTrace();
    } finally {
        manager.close();
    }
}