首页 > 其他 > 详细

在文件列表中选择文件,并把一个文件的内容显示在TextArea中

时间:2015-03-09 10:36:22      阅读:270      评论:0      收藏:0      [点我收藏+]
private FileDialog openFileDialog = new FileDialog(this,"Open File",FileDialog.LOAD);

else if(eventSource == openFile)
        {
            openFileDialog.show();
            fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
            if(fileName != null)
              readFile(fileName);
        }

public void readFile(String fileName)

{

File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead = 0;
char[] content = new char[size];
while(readIn.ready())
charsRead += readIn.read(content, charsRead, size - charsRead);
readIn.close();
textArea.setText(new String(content, 0, charsRead));

}

 基本步骤 :创建一个FileDialog类型的对象,参数为this,string,FileDialog.load

在actionPerformed方法中,若event来源为openfile时,显示文件列表对话框,利用getPath()和getFIle()方法获得完整文件路径名

重写readFile函数,定义File和FileReader对象,int型对象表示文件长度,以及char型的数组

最后关闭FileReader对象

在文件列表中选择文件,并把一个文件的内容显示在TextArea中

原文:http://www.cnblogs.com/zhwtcle/p/4322931.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!