一个简单的读取字符流,显示的例子
[code]
package cn.lokvin.examples.io;
import java.awt.Button;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileViewer extends Frame implements ActionListener {
String directory; // 显示 dialog 的目录
TextArea textarea; // 显示文件内容区域
public FileViewer () {
this(null, null);
}
public FileViewer(String fileName) {
this(null, fileName);
}
public FileViewer(String directory, String fileName) {
super(); //创建 Frame
//添加关闭Frame 响应事件
this.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.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);
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();
}
}
[/code]
[code]
package cn.lokvin.examples.io;
import java.awt.Button;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileViewer extends Frame implements ActionListener {
String directory; // 显示 dialog 的目录
TextArea textarea; // 显示文件内容区域
public FileViewer () {
this(null, null);
}
public FileViewer(String fileName) {
this(null, fileName);
}
public FileViewer(String directory, String fileName) {
super(); //创建 Frame
//添加关闭Frame 响应事件
this.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.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);
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();
}
}
[/code]