Java写的浏览文件内容的窗体
来源:互联网 发布:吉诺比利总决赛数据 编辑:程序博客网 时间:2024/05/11 14:26
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 btOpenFile = new Button("Open File");
Button btClose = new Button("Close");
btOpenFile.addActionListener(this);
btOpenFile.setActionCommand("open");
btOpenFile.setFont(font);
btClose.addActionListener(this);
btClose.setActionCommand("close");
btClose.setFont(font);
p.add(btOpenFile);
p.add(btClose);
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);
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)
{
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();
}
public static void main(String[] args) throws IOException
{
Frame f = new FileViewer((args.length == 1) ? args[0] : null);
f.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
});
f.show();
}
}
- Java写的浏览文件内容的窗体
- 通过窗体输入的内容来新建一个xml文件
- java通过流把一个文件的内容写到另一个文件中
- 在控制台中写的内容写到文件中
- java源码——对文件内容的查找和替换(开始写界面咯)
- html DOM 自学 重点浏览的内容
- Android使用文件管理器打开指定文件夹,浏览里面的内容
- 文件浏览样式的修改
- 一个文件浏览的程序
- PHP实现文件的浏览
- pdf文件的在线浏览
- 文件浏览对话框(旧式的文件浏览对话框)
- 用java.util.Properties类读取属性文件的内容,或者把属性写到xml文件中
- 遇到的问题-streamwriter写文件,内容丢失
- Qt将文本框的内容写到文件里
- ruby写一个文件内容相似性比较的代码
- linux下追加所写的内容到文件内
- 写内容到Windows的文件怎么换行?
- 回溯法 求所有子集 所有排列
- 理解H.264 SVC的MGS 之 结构篇
- 光脚丫学LINQ(017):[演练]跨关系查询(C#):手动编码
- SVC标准G.8.1.4.1中关于ref list construcion的一个问题
- 关于Bit Depth SVC
- Java写的浏览文件内容的窗体
- 《Windows核心编程》の窗口过程的使用
- [腾讯面试]有1到10w这10w个数,去除2个并打乱次序,如何找出那两个数
- 关于Bit Depth SVC
- 文学随笔:《想》
- 自己做美工说不定能用上的资源
- jn
- 嵌入式数据库系统Berkeley DB
- Python核心编程——第三章基础