KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > example > ShowProperties


1 /*
2 * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.example;
17
18 import org.apache.commons.vfs.FileObject;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.FileSystemManager;
21 import org.apache.commons.vfs.FileType;
22 import org.apache.commons.vfs.VFS;
23
24 import java.text.DateFormat JavaDoc;
25 import java.util.Date JavaDoc;
26
27 /**
28  * A simple that prints the properties of the file passed as first parameter.
29  *
30  * @author <a HREF="mailto:anthony@antcommander.com">Anthony Goubard</a>
31  */

32
33
34 public class ShowProperties
35 {
36     public static void main(String JavaDoc[] args)
37     {
38         if (args.length == 0)
39         {
40             System.err.println("Please pass the name of a file as parameter.");
41             System.err.println("e.g. java org.apache.commons.vfs.example.ShowProperties LICENSE.txt");
42             return;
43         }
44         for (int i = 0; i < args.length; i++)
45         {
46             try
47             {
48                 FileSystemManager mgr = VFS.getManager();
49                 System.out.println();
50                 System.out.println("Parsing: " + args[i]);
51                 FileObject file = mgr.resolveFile(args[i]);
52                 System.out.println("URL: " + file.getURL());
53                 System.out.println("getName(): " + file.getName());
54                 System.out.println("BaseName: " + file.getName().getBaseName());
55                 System.out.println("Extension: " + file.getName().getExtension());
56                 System.out.println("Path: " + file.getName().getPath());
57                 System.out.println("Scheme: " + file.getName().getScheme());
58                 System.out.println("URI: " + file.getName().getURI());
59                 System.out.println("Root URI: " + file.getName().getRootURI());
60                 System.out.println("Parent: " + file.getName().getParent());
61                 System.out.println("Type: " + file.getType());
62                 System.out.println("Exists: " + file.exists());
63                 System.out.println("Readable: " + file.isReadable());
64                 System.out.println("Writeable: " + file.isWriteable());
65                 System.out.println("Root path: " + file.getFileSystem().getRoot().getName().getPath());
66                 if (file.exists())
67                 {
68                     if (file.getType().equals(FileType.FILE))
69                     {
70                         System.out.println("Size: " + file.getContent().getSize() + " bytes");
71                     }
72                     else if (file.getType().equals(FileType.FOLDER) && file.isReadable())
73                     {
74                         FileObject[] children = file.getChildren();
75                         System.out.println("Directory with " + children.length + " files");
76                         for (int iterChildren = 0; iterChildren < children.length; iterChildren++)
77                         {
78                             System.out.println("#" + iterChildren + ": " + children[iterChildren].getName());
79                             if (iterChildren > 5)
80                             {
81                                 break;
82                             }
83                         }
84                     }
85                     System.out.println("Last modified: " + DateFormat.getInstance().format(new Date JavaDoc(file.getContent().getLastModifiedTime())));
86                 }
87                 else
88                 {
89                     System.out.println("The file does not exist");
90                 }
91                 file.close();
92             }
93             catch (FileSystemException ex)
94             {
95                 ex.printStackTrace();
96             }
97         }
98     }
99 }
100
101
Popular Tags