java 用文件对话框打开文件
//文件的打开
import java.awt.FileDialog;
import java.awt.event.*;
import java.io.*;
import java.io.File;
import java.io.FileReader;
public class FileOpen {
private FileDialog filedialog_open;
private String fileopen = null, filename = null;// 用于存放打开文件地址 和文件名
private File file1; // 文件字节流对象
private FileReader file_reader;//文件字符流对象
private BufferedReader in;//文件行读取 写入对象
private StringBuffer text = new StringBuffer();
HaffmanFrame haffman= null;
FileOpen(HaffmanFrame hf) {
haffman = hf;
filedialog_open = new FileDialog(haffman, "打开文件对话框", FileDialog.LOAD);
// 打开文件对话框适配器
filedialog_open.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
filedialog_open.setVisible(false);
}
});
}
public void open() {
String s = "";
filedialog_open.setVisible(true);
fileopen = filedialog_open.getDirectory();// 返回文件对话框中显示的文件所属的目录
filename = filedialog_open.getFile();// 返回当前文件对话框中显示的文件名的字符串表示
// 如果不存在就返回NULL
if (filename != null)// 判断打开的文件是否存在
{
try {
file1 = new File(fileopen,filename );
file_reader = new FileReader(file1);
in = new BufferedReader(file_reader);//每次读取一行
while ((s = in.readLine()) != null)
text.append(s + '\n');
in.close();
file_reader.close();
} catch (IOException e2) {
System.out.println("不能打开文件!");
}
}
}
//返回得到的文本字符串
public String getText() {
return new String(text);
}
}
相关文档:
List的用法
List包括List接口以及List接口的所有实现类。因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表类型,所以List接口还提供了一些适合于自身的常用方法,如表1所示。
表1 List接口定义的常用方法及功能
从表1可以看出,List接口提供的适合于自身的 ......
//导入
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat; Date dt=new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//设置显示格式
String nowTime=" ......
Java Servlet API说明文档(2.1a版)(一)
译者前言:
近来在整理有关Servlet资料时发现,在网上竟然找不到一份中文的Java Servlet API的说明文档,而在有一本有关JSP的书后面附的Java Servlet API说明竟然不全,而这份文档的2.1a版在1998年的11月份 ......
//哈弗曼编码的实现类
public class HffmanCoding {
private int charsAndWeight[][];// [][0]是 字符,[][1]存放的是字符的权值(次数)
private int hfmcoding[][];// 存放哈弗曼树
private int i = 0;// 循环变量
private String hcs[];
public HffmanCoding(int[][] chars) {
// TODO 构造方法
......