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: */
017: package org.apache.commons.vfs.example;
018:
019: import org.apache.commons.vfs.FileObject;
020: import org.apache.commons.vfs.FileSystemException;
021: import org.apache.commons.vfs.FileSystemManager;
022: import org.apache.commons.vfs.FileType;
023: import org.apache.commons.vfs.VFS;
024:
025: import java.text.DateFormat;
026: import java.util.Date;
027:
028: /**
029: * A simple that prints the properties of the file passed as first parameter.
030: *
031: * @author <a href="mailto:anthony@antcommander.com">Anthony Goubard</a>
032: * @version $Revision:232419 $ $Date:2005-08-13 07:23:40 +0200 (Sa, 13 Aug 2005) $
033: */
034:
035: public class ShowProperties {
036: public static void main(String[] args) {
037: if (args.length == 0) {
038: System.err
039: .println("Please pass the name of a file as parameter.");
040: System.err
041: .println("e.g. java org.apache.commons.vfs.example.ShowProperties LICENSE.txt");
042: return;
043: }
044: for (int i = 0; i < args.length; i++) {
045: try {
046: FileSystemManager mgr = VFS.getManager();
047: System.out.println();
048: System.out.println("Parsing: " + args[i]);
049: FileObject file = mgr.resolveFile(args[i]);
050: System.out.println("URL: " + file.getURL());
051: System.out.println("getName(): " + file.getName());
052: System.out.println("BaseName: "
053: + file.getName().getBaseName());
054: System.out.println("Extension: "
055: + file.getName().getExtension());
056: System.out.println("Path: " + file.getName().getPath());
057: System.out.println("Scheme: "
058: + file.getName().getScheme());
059: System.out.println("URI: " + file.getName().getURI());
060: System.out.println("Root URI: "
061: + file.getName().getRootURI());
062: System.out.println("Parent: "
063: + file.getName().getParent());
064: System.out.println("Type: " + file.getType());
065: System.out.println("Exists: " + file.exists());
066: System.out.println("Readable: " + file.isReadable());
067: System.out.println("Writeable: " + file.isWriteable());
068: System.out.println("Root path: "
069: + file.getFileSystem().getRoot().getName()
070: .getPath());
071: if (file.exists()) {
072: if (file.getType().equals(FileType.FILE)) {
073: System.out.println("Size: "
074: + file.getContent().getSize()
075: + " bytes");
076: } else if (file.getType().equals(FileType.FOLDER)
077: && file.isReadable()) {
078: FileObject[] children = file.getChildren();
079: System.out.println("Directory with "
080: + children.length + " files");
081: for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
082: System.out.println("#" + iterChildren
083: + ": "
084: + children[iterChildren].getName());
085: if (iterChildren > 5) {
086: break;
087: }
088: }
089: }
090: System.out.println("Last modified: "
091: + DateFormat.getInstance().format(
092: new Date(file.getContent()
093: .getLastModifiedTime())));
094: } else {
095: System.out.println("The file does not exist");
096: }
097: file.close();
098: } catch (FileSystemException ex) {
099: ex.printStackTrace();
100: }
101: }
102: }
103: }
|