⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileviewer.java

📁 文件目录显示,并能输出txt本文文件(显示出来各文件夹下所有文件名称等)
💻 JAVA
字号:
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();
			}
		});
		
		textarea = new TextArea("",24,80);
		textarea.setFont(new Font("MonoSpaced",Font.PLAIN,12));
		textarea.setEditable(false);
		
		this.add("Center",textarea);
		Panel p  = new Panel();
		p.setLayout(new FlowLayout(FlowLayout.RIGHT,10,5));
		this.add(p,"South");
		
		Font font = new Font("SansSerif",Font.BOLD,14);
		Button openfile = new Button("Open File");
		Button close = new Button("Close");
		openfile.addActionListener(this);
		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);
		}
		// TODO 自动生成构造函数存根
	}

	private void setFile(String directory2, String filename) {
		// TODO 自动生成方法存根
		if((filename==null)||(filename.length()==0))return;
		File f;
		FileReader in = null;
		try{
			f = new File(directory,filename);
			in = new FileReader(f);
			char[] buffer = new char[4096];
			int len;
			textarea.setText("");
			while((len=in.read(buffer))!=-1){
				String s = new String (buffer,0,len);
				textarea.append(s);
				
			}
			this.setTitle("FileViewer: "+filename);
			textarea.setCaretPosition(0);
			     }catch(IOException e){
			    	 textarea.setText(e.getClass().getName()+": "+e.getMessage());
			    	 this.setTitle("FileViewer: "+filename+": I/O Exception");
			  
			     }
			     finally{
			    	 try{
			    		 if(in !=null) in.close();
			    	 }catch(IOException e){
			    		 
			    	 }
			     }
	}

	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成方法存根
		String cmd = e.getActionCommand();
		if(cmd.equals("open")){
			FileDialog f = new FileDialog(this,"Open File",FileDialog.LOAD);
			f.setDirectory(directory);
			f.show();
			directory = f.getDirectory();
			setFile(directory,f.getFile());
			f.dispose();
		}
		else{
			if(cmd.equals("close")){
				this.dispose();
			}
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -