Java实现目录查找与文本阅读
2009-10-25 20:53:54 来源:WEB开发网1.实现文本阅读
package AllAboutFile;
/**
*
* @author lucifer
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileViewer extends Frame implements ActionListener{
String directory;
TextArea textarea; //显示文本内容的地方
public FileViewer(){
this(null,null);
}
public FileViewer(String filename){
this(null,filename);
}
public FileViewer(String directory,String filename){
super();
addWindowListener(
new WindowAdapter(){
public void WindowClosing(WindowEvent e){
dispose();
}
}
);
//Create TextArea
textarea = new TextArea("",24,80);
textarea.setFont(new Font("Monospaced",Font.PLAIN,12));
textarea.setEditable( false );
this.add("Center",textarea);
//Create Panel
Panel p = new Panel();
p.setLayout( new FlowLayout(FlowLayout.RIGHT,10,5) );
this.add(p,"South");
//Create Button
Font font = new Font("SansSerif",Font.BOLD,14);
Button OpenFile = new Button("Open File");
Button Close = new Button("Close");
OpenFile.addActionListener(this);
OpenFile.setActionCommand("open");
OpenFile.setFont(font);
Close.addActionListener( this );
Close.setActionCommand( "close" );
Close.setFont( font );
p.add( OpenFile );
p.add( Close );
this.pack();
if(directory == null){
File f;
if((filename != null)&&(f = new File(filename)).isAbsolute()){
directory = f.getParent();
filename = f.getName();
}
else
directory = System.getPRoperty( "user.dir" );
}
this.directory = directory;
setFile(directory,filename);
}
public void setFile(String directory,String filename){
if((filename == null)||(filename.length() == 0)){
return;
}
File f = new File(directory,filename);
try{
FileReader fr = new FileReader(f);
char buffer[] = new char[4096];
int len;
textarea.setText( "" );
//show text
while((len = fr.read(buffer)) != -1){
String str = new String(buffer,0,len);
textarea.append( str );
}
this.setTitle( "FileViewer:" + filename );
textarea.setCaretPosition( 0 );
fr.close();
}
catch(Exception e){
textarea.setText( e.getMessage() );
}
}
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if(cmd.equals( "open" )){
FileDialog fd = new FileDialog(this,"Select File",FileDialog.LOAD);
fd.setDirectory( directory );
fd.show();
directory = fd.getDirectory();
setFile(directory,fd.getFile());
fd.dispose();
}
else if(cmd.equals( "close" )){
this.dispose();
}
}
static public void main(String[] args){
Frame f = new FileViewer((args.length==1)?args[0]:null);
f.addWindowListener(new WindowAdapter(){
public void WindowClosed(WindowEvent e){
System.exit(0);
}
}
);
f.show();
}
}
2.目录显示与操作
package AllAboutFile;
/**
*
* @author lucifer
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.DateFormat;
public class FileLister extends Frame implements ActionListener,ItemListener{
private List list;
private TextField details;
private Panel buttons;
private Button up,close;
private File currentDir;
private FilenameFilter filter;
private String[] files;
private DateFormat dateFormatter =
DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);
public FileLister(String directory,FilenameFilter filter){
super("File Lister");
this.filter = filter;
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
dispose();
}
});
list = new List(12,false);
list.setFont( new Font("MonoSpaced",Font.PLAIN,14) );
list.addActionListener( this );
list.addItemListener( this );
details = new TextField();
details.setFont( new Font("MonoSpaced",Font.PLAIN,12) );
details.setEditable(false);
buttons = new Panel();
buttons.setLayout(new FlowLayout(FlowLayout.RIGHT,15,5));
buttons.setFont( new Font("SansSerif",Font.BOLD,14) );
up = new Button("Upper Directory");
close = new Button("Close");
up.addActionListener( this );
close.addActionListener( this );
buttons.add( up );
buttons.add( close );
this.add( list, "Center" );
this.add( details, "North" );
this.add( buttons , "South" );
this.setSize( 500 , 350);
listDirectory(directory);
}
public void listDirectory(String directory){
File dir = new File(directory);
if(!dir.isDirectory())
throw new IllegalArgumentException("FileListener : no such firectory");
files = dir.list( filter );
java.util.Arrays.sort( files );
list.removeAll();
list.add( "[Up to Parent Directory]" );
for(int i = 0;i < files.length; i++){
list.add( files[i] );
}
this.setTitle( directory );
details.setText( directory );
currentDir = dir;
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == close)
this.dispose();
else if(ae.getSource() == up)
up();
else if(ae.getSource() == list){
int i = list.getSelectedIndex();
if( i == 0 )
up();
else{
String name = files[i-1];
File f = new File(currentDir,name);
String fullname = f.getAbsolutePath();
if(f.isDirectory())
listDirectory(fullname);
else new FileViewer(fullname).show();
}
}
}
public void itemStateChanged(ItemEvent ie){
int i = list.getSelectedIndex() - 1;
if(i < 0)
return;
String filename = files[i];
File f = new File(currentDir,filename);
if(!f.exists())
throw new IllegalArgumentException("FileLister : no such file or directory");
String info = null;
if(f.isDirectory())
info += File.separator;
info += " " + f.length() + " bytes ";
info += dateFormatter.format( new java.util.Date(f.lastModified()) );
if(f.canRead())
info += " Read";
if(f.canWrite())
info += " Write";
details.setText(info);
}
protected void up(){
String parent = currentDir.getParent();
if(parent == null)
return;
listDirectory(parent);
}
public static void usage(){
System.out.println("Usage : java FileLister [directory_name] " +
"[-e file_extension]");
System.exit( 0 );
}
public static void main(String[] args){
FileLister f;
FilenameFilter filter = null;
String directory = null;
for(int i = 0;i < args.length; i++){
if(args[i].equals("-e")){
if(++i > args.length)
usage();
final String suffix = args[i];
filter = new FilenameFilter(){
public boolean accept(File dir,String name){
if(name.endsWith( suffix ))
return true;
else
return (new File(dir,name)).isDirectory();
}
};
}
else{
if(directory != null)
usage();
else directory = args[i];
}
}
if(directory == null)
directory = System.getProperty( "user.dir" );
f = new FileLister(directory,filter);
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosed(WindowEvent e){
System.exit(0);
}
});
f.show();
}
}
更多精彩
赞助商链接