View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.example;
18  
19  import java.text.DateFormat;
20  import java.util.Date;
21  
22  import org.apache.commons.vfs2.FileObject;
23  import org.apache.commons.vfs2.FileSystemException;
24  import org.apache.commons.vfs2.FileSystemManager;
25  import org.apache.commons.vfs2.FileType;
26  import org.apache.commons.vfs2.VFS;
27  
28  /**
29   * Example which prints all properties of the file passed as first parameter.
30   */
31  public final class ShowProperties {
32      /** Maximum number of children to show. */
33      private static final int SHOW_MAX = 5;
34  
35      private ShowProperties() {
36          /* main class not instantiated. */
37      }
38  
39      public static void main(final String[] args) {
40          if (args.length == 0) {
41              System.err.println("Please pass the name of a file as parameter.");
42              System.err.println("e.g. java org.apache.commons.vfs2.example.ShowProperties LICENSE.txt");
43              return;
44          }
45          for (final String arg : args) {
46              try {
47                  final FileSystemManager mgr = VFS.getManager();
48                  System.out.println();
49                  System.out.println("Parsing: " + arg);
50                  final FileObject file = mgr.resolveFile(arg);
51                  System.out.println("URL: " + file.getURL());
52                  System.out.println("getName(): " + file.getName());
53                  System.out.println("BaseName: " + file.getName().getBaseName());
54                  System.out.println("Extension: " + file.getName().getExtension());
55                  System.out.println("Path: " + file.getName().getPath());
56                  System.out.println("Scheme: " + file.getName().getScheme());
57                  System.out.println("URI: " + file.getName().getURI());
58                  System.out.println("Root URI: " + file.getName().getRootURI());
59                  System.out.println("Parent: " + file.getName().getParent());
60                  System.out.println("Type: " + file.getType());
61                  System.out.println("Exists: " + file.exists());
62                  System.out.println("Readable: " + file.isReadable());
63                  System.out.println("Writeable: " + file.isWriteable());
64                  System.out.println("Root path: " + file.getFileSystem().getRoot().getName().getPath());
65                  if (file.exists()) {
66                      if (file.getType().equals(FileType.FILE)) {
67                          System.out.println("Size: " + file.getContent().getSize() + " bytes");
68                      } else if (file.getType().equals(FileType.FOLDER) && file.isReadable()) {
69                          final FileObject[] children = file.getChildren();
70                          System.out.println("Directory with " + children.length + " files");
71                          for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
72                              System.out.println("#" + iterChildren + ": " + children[iterChildren].getName());
73                              if (iterChildren > SHOW_MAX) {
74                                  break;
75                              }
76                          }
77                      }
78                      System.out.println("Last modified: "
79                              + DateFormat.getInstance().format(new Date(file.getContent().getLastModifiedTime())));
80                  } else {
81                      System.out.println("The file does not exist");
82                  }
83                  file.close();
84              } catch (final FileSystemException ex) {
85                  ex.printStackTrace();
86              }
87          }
88      }
89  
90  }