import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import java.io.*;
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();
}
/**
* 设置浏览的路径
*/
public void setPath(String path){
this.path=path;
} /***
* 返回当前目录路径
*/
public String getDirectory(){
return myDir.getPath();
} /**
* 刷新列表
*/
public void refreshList(){
if(this.path.equals("")) path="c:\\";
myDir=new File(path); vectorList.clear();
contents =myDir.listFiles();
//重新装入路径下文件
for(int i=0;i<contents.length ;i++){//tjy
vectorList.add(contents[i]);
}
currentFileView=vectorList.iterator();
}
/**
* 移动当前文件集合的指针指到下一个条目
* @return 成功返回true,否则false
*/
public boolean nextFile(){
while(currentFileView.hasNext()){
currentFile=(File)currentFileView.next();
return true;
}
return false;
} /**
* 返回当前指向的文件对象的文件名称
*/
public String getFileName(){
return currentFile.getName();
}
/**
* 返回当前指向的文件对象的文件尺寸
*/
public String getFileSize(){
return new Long(currentFile.length()).toString();
} /**
* 返回当前指向的文件对象的最后修改日期
*/
public String getFileTimeStamp(){
return new Date(currentFile.lastModified()).toString();
} /**
* 返回当前指向的文件对象是否是一个文件目录
*/
public boolean getFileType(){
return currentFile.isDirectory();
}
}public class FileViewerTest{
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");
}
}
}
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import java.io.*;
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();
}
/**
* 设置浏览的路径
*/
public void setPath(String path){
this.path=path;
} /***
* 返回当前目录路径
*/
public String getDirectory(){
return myDir.getPath();
} /**
* 刷新列表
*/
public void refreshList(){
if(this.path.equals("")) path="c:\\";
myDir=new File(path); vectorList.clear();
contents =myDir.listFiles();
//重新装入路径下文件
for(int i=0;i<contents.length ;i++){//tjy
vectorList.add(contents[i]);
}
currentFileView=vectorList.iterator();
}
/**
* 移动当前文件集合的指针指到下一个条目
* @return 成功返回true,否则false
*/
public boolean nextFile(){
while(currentFileView.hasNext()){
currentFile=(File)currentFileView.next();
return true;
}
return false;
} /**
* 返回当前指向的文件对象的文件名称
*/
public String getFileName(){
return currentFile.getName();
}
/**
* 返回当前指向的文件对象的文件尺寸
*/
public String getFileSize(){
return new Long(currentFile.length()).toString();
} /**
* 返回当前指向的文件对象的最后修改日期
*/
public String getFileTimeStamp(){
return new Date(currentFile.lastModified()).toString();
} /**
* 返回当前指向的文件对象是否是一个文件目录
*/
public boolean getFileType(){
return currentFile.isDirectory();
}
}public class FileViewerTest{
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");
}
}
}
解决方案 »
- SQLException:将截断字符串或二进制数据
- 数据库 请高手指点
- 大家帮忙看看这个错在哪里了!
- 自己写对话框
- java实现telnet功能
- 怎么这代码不能显示按钮?是不是一定要jpanel?
- 看原代码FileSystem.java时看到奇怪(小弟太菜,只能用"奇怪")的static的用法,请大家帮忙解释一下,谢谢!
- 关于面向对象 做一个即时通讯平台
- 我认为自己的jdk已经配好了,为什么会出现以下问题:exception in thread "main" java.lang.nosuchmethoderror: main
- taglib重定向问题:请问taglib的Class类中可以重定向到某个页面吗?怎么写呢?
- 请教各位:关于JAVA.url包的问题
- 一个页中怎样插入多个Applet,两个Applet之间怎样进行数据等通信呢。。。
File[] flist;
File a=new File("c:\\");
flist=a.listFiles();
for(int i=0;i<flist.length;i++)
System.out.println(flist[i]);
File[] files = (new File(path)).listFiles();
File temp;
for(int i=0;i<files.length;i++){
for(int j=i;j<files.length;j++){
if(files[i].getName().compareTo(files[j].getName())>0){
temp = files[i];
files[i]=files[j];
files[j]=temp;
}//if//
}
}
for(int k=0;k<files.length;k++){
System.out.println(files[k].getName());
}
按文件名排序