📄 fileviewer.java
字号:
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class FileViewer extends Frame implements ActionListener{
//继承了Frame类并实现ActionListener接口
/**
* @param args
*/
String directory;//要阅读的文件所在的目录
TextArea textarea;//显示文件内容用
//下面接连定义了三个构造函数,稍后我们看一下分别利用这三个构造函数创建对象的时候,会有什么不同。
public FileViewer(){
this(null,null);//如果没有指定参数,即目录为空、文件名为空
}
public FileViewer(String filename){
this(null,filename);//指定了文件名,但没有指定目录
}
public FileViewer(String directory,String filename){
super();//创建一个Frame对象
addWindowListener(new WindowAdapter(){//很熟悉的关闭按钮事件捕捉
public void windowClosing(WindowEvent e){
dispose();//销毁窗口,也可以使用System.exit(0);
}
});
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.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;
FileReader in=null;//创建文件读入器(按字符读取)
try{
f=new File(directory,filename);//创建文件对象
in=new FileReader(f);//读入文件对象f的内容到读入器
char[] buffer=new char[4096];//缓冲
int len;
textarea.setText("");//默认为空
while((len=in.read(buffer))!=-1){//如果有内容可读
String s=new String(buffer,0,len);//将buffer中的内容转换为String类型
textarea.append(s);
//读取buffer中的内容,读取的内容为从0开始至可读取的字符数,在使用in.reader(buffer)读入的时候会返回下一个读取的字符数。我们可以在这下面增加一行“System.out.println(""+len);”,并且把buffer数组设置为new char[10],我们发现,读取的时候是一段一段读取的,同时我们可以看到Console中输出的依次是10、10……,最后是一个小于10的数,例如6,由此可见,len返回的是buffer中的字符数,FileReader读取的过程是这样的:流放入10个字符到buffer,然后FileReader一次性读取完毕一个buffer(这个时候流也没有停,因为它不能停,但内容会放到另外的地方),读取完之后FileReader又接着读取下一个位置的buffer……
}
this.setTitle("FileViewer: "+filename);//用文件名作为窗口的标题
textarea.setCaretPosition(0);//javadoc中是这样描述的:Sets the position of the text insertion caret. The caret position is constrained to be between 0 and the last character of the text, inclusive. If the passed-in value is greater than this range, the value is set to the last character (or 0 if the TextComponent contains no text) and no error is returned. If the passed-in value is less than 0, an IllegalArgumentException is thrown. 看不太懂,明天再搞明白
}
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){
String cmd=e.getActionCommand();
if(cmd.equals("open")){
FileDialog f=new FileDialog(this,"Open File",FileDialog.LOAD);
//创建一个FileDialog对象,类型为FileDialog.LOAD,即打开文件窗口
f.setDirectory(directory);//打开文件窗口中默认的目录,即为上面设置的位置,即如果有输入按输入,没有输入按当前目录
//f.show();//这个已经过期,不推荐使用
f.setVisible(true);
directory=f.getDirectory();//directory最终已这里设定的为准
setFile(directory,f.getFile());//利用上面定义的setFile显示指定的文件内容
f.dispose();
}
else if(cmd.equals("close"))
this.dispose();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f=new FileViewer((args.length==1)?args[0]:null);//这里用到的是有一个参数的那个构造函数,如果Console中输入的参数为1个,则将其赋给参数filename
f.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
System.exit(0);
}
});
//f.show();
f.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -