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 * A simple that prints the properties of the file passed as first parameter.
030 */
031
032
033public class ShowProperties
034{
035    public static void main(final String[] args)
036    {
037        if (args.length == 0)
038        {
039            System.err.println("Please pass the name of a file as parameter.");
040            System.err.println("e.g. java org.apache.commons.vfs2.example.ShowProperties LICENSE.txt");
041            return;
042        }
043        for (final String arg : args)
044        {
045            try
046            {
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                {
067                    if (file.getType().equals(FileType.FILE))
068                    {
069                        System.out.println("Size: " + file.getContent().getSize() + " bytes");
070                    }
071                    else if (file.getType().equals(FileType.FOLDER) && file.isReadable())
072                    {
073                        final FileObject[] children = file.getChildren();
074                        System.out.println("Directory with " + children.length + " files");
075                        for (int iterChildren = 0; iterChildren < children.length; iterChildren++)
076                        {
077                            System.out.println("#" + iterChildren + ": " + children[iterChildren].getName());
078                            if (iterChildren > 5)
079                            {
080                                break;
081                            }
082                        }
083                    }
084                    System.out.println("Last modified: " + DateFormat.getInstance().format(
085                            new Date(file.getContent().getLastModifiedTime())));
086                }
087                else
088                {
089                    System.out.println("The file does not exist");
090                }
091                file.close();
092            }
093            catch (final FileSystemException ex)
094            {
095                ex.printStackTrace();
096            }
097        }
098    }
099}
100