Read the file list of various computer file systems with java

xiaoxiao2021-03-06  61

Author: jackliu

Email: suntoday@eyou.com

When you install JDK, in the JDK class library, there is a standard File class. Through this class, you can easily browse the functions of various file systems.

File class:

Java.lang.object

|

- java.io.file

The FILE class extends the Object object, implements the interface of Serializable, Comparable definition, can make a variety of operations on the JVM on an instance of File, and I have written a FileViewer class, which will list a list through the Iterator class. Realize iterative operations

FileViewer.java

Import java.io.file;

Import java.util.date;

Import java.util.iterator;

Import java.util.vector;

Public class fileviewer {

File mydir;

File [] contents;

Vector vectorList;

Iterator currentfileView;

File Currentfile;

String path;

Public FileViewer () {

Path = new string ("");

VectorList = new vector ();

}

Public FileViewer (String path) {

THIS.PATH = PATH;

VectorList = new vector ();

}

/ **

* Set the path to the browse

* /

Public void setpath (string path) {

THIS.PATH = PATH;

}

/ ***

* Return to the current directory path

* /

Public string getdirectory () {

Return mydir.getpath ();

}

/ **

* refresh the list

* /

Public void refreshlist () {

IF (this.path.equals (")) PATH =" C: // ";

mydir = new file (path);

VectorList.clear ();

CONTENTS = MyDir.listfiles ();

// Replace the path under the path

For (int i = 0; i

VectorList.Add (Contents [i]);

}

CurrentFileView = VectorList.ITerator ();

}

/ **

* Move the pointer to the current file collection points to the next entry

* @Return successfully returns true, otherwise false

* /

Public boolean nextfile () {

While (currentfileview.hasnext ()) {

Currentfile = (file) currentfileView.next ();

Return True;

}

Return False;

}

/ **

* Return to the file name of the file object pointing

* /

Public string getFileName () {

Return CurrentFile.getName ();

}

/ **

* Return to the file size of the currently pointing file object

* /

Public string getFilesize () {

Return new long (currentfile.length ()). TOSTRING ();

}

/ **

* Returns the last modification date of the current pointing file object

* /

Public string getFiletimeStamp () {

Return New Date (currentfile.lastmodified ()). Tostring ();

/ **

* Return to whether the current pointing file object is a file directory

* /

Public Boolean getFileType () {

Return Currentfile.Indirectory ();

}

}

Set the directory to be browsed by setPath () method (note if the operating system is a Microsoft operating system, each path separator should be written into two slashes //), the nextfile () method is used to move the list record, which can pass GetFileName ) Get file or folder name, get the file size via getFileSize (), to get the final modification time of the file via getFileTimeStamp (), and determine whether it is a file directory via getFileType ().

Write a Test example to test this FileViewer class

Test.java

Import java.io. *;

Public class test {

Public static void main (String [] args) {

System.out.println ("File List");

FileViewer f = new fileviewer ();

F.SetPath ("D: //");

F.Refreshlist ();

While (f.nextfile ()) {

System.out.print (f.GetFileName ());

IF (! f.Getfiletype ())

System.out.print (" f.GetFileSize ());

Else

System.out.print ("

");

System.out.print (f.GetFiletimeStamp () "/ n");

}

}

}

Package download code

转载请注明原文地址:https://www.9cbs.com/read-112289.html

New Post(0)