001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.example;
018
019import java.text.DateFormat;
020import java.util.Date;
021
022import org.apache.commons.vfs2.FileObject;
023import org.apache.commons.vfs2.FileSystemException;
024import org.apache.commons.vfs2.FileSystemManager;
025import org.apache.commons.vfs2.FileType;
026import org.apache.commons.vfs2.VFS;
027
028/**
029 * Example which prints all properties of the file passed as first parameter.
030 */
031public final class ShowProperties {
032    /** Maximum number of children to show. */
033    private static final int SHOW_MAX = 5;
034
035    private ShowProperties() {
036        /* main class not instantiated. */
037    }
038
039    public static void main(final String[] args) {
040        if (args.length == 0) {
041            System.err.println("Please pass the name of a file as parameter.");
042            System.err.println("e.g. java org.apache.commons.vfs2.example.ShowProperties LICENSE.txt");
043            return;
044        }
045        for (final String arg : args) {
046            try {
047                final FileSystemManager mgr = VFS.getManager();
048                System.out.println();
049                System.out.println("Parsing: " + arg);
050                final FileObject file = mgr.resolveFile(arg);
051                System.out.println("URL: " + file.getURL());
052                System.out.println("getName(): " + file.getName());
053                System.out.println("BaseName: " + file.getName().getBaseName());
054                System.out.println("Extension: " + file.getName().getExtension());
055                System.out.println("Path: " + file.getName().getPath());
056                System.out.println("Scheme: " + file.getName().getScheme());
057                System.out.println("URI: " + file.getName().getURI());
058                System.out.println("Root URI: " + file.getName().getRootURI());
059                System.out.println("Parent: " + file.getName().getParent());
060                System.out.println("Type: " + file.getType());
061                System.out.println("Exists: " + file.exists());
062                System.out.println("Readable: " + file.isReadable());
063                System.out.println("Writeable: " + file.isWriteable());
064                System.out.println("Root path: " + file.getFileSystem().getRoot().getName().getPath());
065                if (file.exists()) {
066                    if (file.getType().equals(FileType.FILE)) {
067                        System.out.println("Size: " + file.getContent().getSize() + " bytes");
068                    } else if (file.getType().equals(FileType.FOLDER) && file.isReadable()) {
069                        final FileObject[] children = file.getChildren();
070                        System.out.println("Directory with " + children.length + " files");
071                        for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
072                            System.out.println("#" + iterChildren + ": " + children[iterChildren].getName());
073                            if (iterChildren > SHOW_MAX) {
074                                break;
075                            }
076                        }
077                    }
078                    System.out.println("Last modified: "
079                            + DateFormat.getInstance().format(new Date(file.getContent().getLastModifiedTime())));
080                } else {
081                    System.out.println("The file does not exist");
082                }
083                file.close();
084            } catch (final FileSystemException ex) {
085                ex.printStackTrace();
086            }
087        }
088    }
089
090}