怎么让文件的内容在textArea中显示出来?
2009-05-05 20:20
381 查看
package javanotpad;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MenuFrame extends JFrame implements ActionListener
{
private static final long serialVersionUID = 201269579650631599L;
private String filePath;
@SuppressWarnings("static-access")
public void initialize()
{
initMenuBar();
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setTitle("Java ");// 设置窗口标题
this.setSize(600, 700);// 设置窗口大小
this.setLocationRelativeTo(null);// 设置窗口位置
this.setVisible(true);// 设置窗口可见性
this.add(new TextArea());// 文本编辑区域
}
@SuppressWarnings("static-access")
public void initMenuBar()
{
JMenuBar menuBar = new JMenuBar();
final JFileChooser fileChoose = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("*.txt", "all file");
// fileChoose.addChoosableFileFilter(filter);
JMenu file, edit, view, search, document, tool, window, help;
JMenuItem fileItem1, fileItem2, fileItem3, fileItem4, fileItem5, editItem1, editItem2, editItem3, helpItem1;
file = new JMenu("文件(F)");// 文件菜单
edit = new JMenu("编辑(E)");// 编辑菜单
view = new JMenu("视图(V)");// 视图菜单
search = new JMenu("搜索(S)");// 上搜菜单
document = new JMenu("文档(D)");// 文档菜单
tool = new JMenu("工具(T)");// 工具菜单
window = new JMenu("窗口(W)");// 窗口菜单
help = new JMenu("帮助(H)");// 帮助菜单
// 文件菜单项
fileItem1 = new JMenuItem("新建");
fileItem2 = new JMenuItem("打开");
fileItem3 = new JMenuItem("保存");
fileItem4 = new JMenuItem("另存为");
fileItem5 = new JMenuItem("退出");
file.add(fileItem1);// 添加文件子菜单项,新建
file.add(fileItem2);// 添加文件子菜单项,打开
file.add(fileItem3);// 添加文件子菜单项,保存
file.add(fileItem4);// 添加文件子菜单项,另存为
file.add(fileItem5);// 添加文件子菜单项,退出
// 编辑菜单项
editItem1 = new JMenuItem("剪切");
editItem2 = new JMenuItem("粘贴");
editItem3 = new JMenuItem("复制");
edit.add(editItem1);// 添加编辑子菜单项,剪切
edit.add(editItem2);// 添加编辑子菜单项,粘贴
edit.add(editItem3);// 添加编辑子菜单项,复制
// 帮助菜单项
helpItem1 = new JMenuItem("关于");
help.add(helpItem1);// 添加帮助子菜单项,关于
helpItem1.addActionListener(this);// 添加关于按钮的事件
menuBar.add(file);// 添加文件菜单
menuBar.add(edit);// 添加编辑菜单
menuBar.add(view);// 添加视图菜单
menuBar.add(search);// 添加搜索菜单
menuBar.add(document);// 添加文档长度
menuBar.add(tool);// 添加工具菜单
menuBar.add(window);// 添加窗口
menuBar.add(help);// 添加帮助菜单
this.setJMenuBar(menuBar);// 添加菜单栏
fileChoose.setFileFilter(filter);// 设置打开文件的类型
fileItem2.addActionListener(new ActionListener()// 添加打开文件对话框事件
{
@Override
public void actionPerformed(ActionEvent e)
{
int returnVal = fileChoose
.showOpenDialog(MenuFrame.this);
if (returnVal == fileChoose.APPROVE_OPTION)
{
filePath = fileChoose.getSelectedFile()
.getAbsolutePath();
try
{
readFile(filePath);
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
}
}
});
fileItem3.addActionListener(new ActionListener()// 添加保存文件对话框
{
@Override
public void actionPerformed(ActionEvent e)
{
int returnVal = fileChoose
.showSaveDialog(MenuFrame.this);
if (returnVal == fileChoose.APPROVE_OPTION)
{
filePath = fileChoose.getSelectedFile()
.getAbsolutePath();
try
{
writerFile(filePath);
} catch (IOException e1)
{
e1.printStackTrace();
}
} else
{
JOptionPane.showMessageDialog(null,"文件将默认保存在:/r/n"+filePath,
"提示",JOptionPane.INFORMATION_MESSAGE);
}
}
});
fileItem5.addActionListener(new ActionListener()// 添加退出程序事件
{
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
@Override
public void actionPerformed(ActionEvent e)// 关于菜单项信息
{
JOptionPane.showMessageDialog(null, "2009-5-4 11:01:29", "关于",
JOptionPane.INFORMATION_MESSAGE);
// JOptionPane.showInternalMessageDialog(null, "2009年5月5日17:22:47", "关于",
// JOptionPane.INFORMATION_MESSAGE);
}
void readFile(String path) throws FileNotFoundException// 显示打开文件的内容
{
File f = new File(path);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s;
try
{
s = br.readLine();
while (s != null)
{
s += s;
System.out.println(s);
s = br.readLine();
}
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (br != null)
{
br.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
try
{
if (fr != null)
{
fr.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
void writerFile(String path) throws IOException// 保存文件内容
{
// File file = new File(path);
// FileWriter fw = new FileWriter(file);
// BufferedWriter bw = new BufferedWriter(fw);
}
}
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MenuFrame extends JFrame implements ActionListener
{
private static final long serialVersionUID = 201269579650631599L;
private String filePath;
@SuppressWarnings("static-access")
public void initialize()
{
initMenuBar();
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setTitle("Java ");// 设置窗口标题
this.setSize(600, 700);// 设置窗口大小
this.setLocationRelativeTo(null);// 设置窗口位置
this.setVisible(true);// 设置窗口可见性
this.add(new TextArea());// 文本编辑区域
}
@SuppressWarnings("static-access")
public void initMenuBar()
{
JMenuBar menuBar = new JMenuBar();
final JFileChooser fileChoose = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("*.txt", "all file");
// fileChoose.addChoosableFileFilter(filter);
JMenu file, edit, view, search, document, tool, window, help;
JMenuItem fileItem1, fileItem2, fileItem3, fileItem4, fileItem5, editItem1, editItem2, editItem3, helpItem1;
file = new JMenu("文件(F)");// 文件菜单
edit = new JMenu("编辑(E)");// 编辑菜单
view = new JMenu("视图(V)");// 视图菜单
search = new JMenu("搜索(S)");// 上搜菜单
document = new JMenu("文档(D)");// 文档菜单
tool = new JMenu("工具(T)");// 工具菜单
window = new JMenu("窗口(W)");// 窗口菜单
help = new JMenu("帮助(H)");// 帮助菜单
// 文件菜单项
fileItem1 = new JMenuItem("新建");
fileItem2 = new JMenuItem("打开");
fileItem3 = new JMenuItem("保存");
fileItem4 = new JMenuItem("另存为");
fileItem5 = new JMenuItem("退出");
file.add(fileItem1);// 添加文件子菜单项,新建
file.add(fileItem2);// 添加文件子菜单项,打开
file.add(fileItem3);// 添加文件子菜单项,保存
file.add(fileItem4);// 添加文件子菜单项,另存为
file.add(fileItem5);// 添加文件子菜单项,退出
// 编辑菜单项
editItem1 = new JMenuItem("剪切");
editItem2 = new JMenuItem("粘贴");
editItem3 = new JMenuItem("复制");
edit.add(editItem1);// 添加编辑子菜单项,剪切
edit.add(editItem2);// 添加编辑子菜单项,粘贴
edit.add(editItem3);// 添加编辑子菜单项,复制
// 帮助菜单项
helpItem1 = new JMenuItem("关于");
help.add(helpItem1);// 添加帮助子菜单项,关于
helpItem1.addActionListener(this);// 添加关于按钮的事件
menuBar.add(file);// 添加文件菜单
menuBar.add(edit);// 添加编辑菜单
menuBar.add(view);// 添加视图菜单
menuBar.add(search);// 添加搜索菜单
menuBar.add(document);// 添加文档长度
menuBar.add(tool);// 添加工具菜单
menuBar.add(window);// 添加窗口
menuBar.add(help);// 添加帮助菜单
this.setJMenuBar(menuBar);// 添加菜单栏
fileChoose.setFileFilter(filter);// 设置打开文件的类型
fileItem2.addActionListener(new ActionListener()// 添加打开文件对话框事件
{
@Override
public void actionPerformed(ActionEvent e)
{
int returnVal = fileChoose
.showOpenDialog(MenuFrame.this);
if (returnVal == fileChoose.APPROVE_OPTION)
{
filePath = fileChoose.getSelectedFile()
.getAbsolutePath();
try
{
readFile(filePath);
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
}
}
});
fileItem3.addActionListener(new ActionListener()// 添加保存文件对话框
{
@Override
public void actionPerformed(ActionEvent e)
{
int returnVal = fileChoose
.showSaveDialog(MenuFrame.this);
if (returnVal == fileChoose.APPROVE_OPTION)
{
filePath = fileChoose.getSelectedFile()
.getAbsolutePath();
try
{
writerFile(filePath);
} catch (IOException e1)
{
e1.printStackTrace();
}
} else
{
JOptionPane.showMessageDialog(null,"文件将默认保存在:/r/n"+filePath,
"提示",JOptionPane.INFORMATION_MESSAGE);
}
}
});
fileItem5.addActionListener(new ActionListener()// 添加退出程序事件
{
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
@Override
public void actionPerformed(ActionEvent e)// 关于菜单项信息
{
JOptionPane.showMessageDialog(null, "2009-5-4 11:01:29", "关于",
JOptionPane.INFORMATION_MESSAGE);
// JOptionPane.showInternalMessageDialog(null, "2009年5月5日17:22:47", "关于",
// JOptionPane.INFORMATION_MESSAGE);
}
void readFile(String path) throws FileNotFoundException// 显示打开文件的内容
{
File f = new File(path);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s;
try
{
s = br.readLine();
while (s != null)
{
s += s;
System.out.println(s);
s = br.readLine();
}
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (br != null)
{
br.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
try
{
if (fr != null)
{
fr.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
void writerFile(String path) throws IOException// 保存文件内容
{
// File file = new File(path);
// FileWriter fw = new FileWriter(file);
// BufferedWriter bw = new BufferedWriter(fw);
}
}
相关文章推荐
- 怎么让文件的内容在textArea中显示出来?
- 从键盘输入若干个字符,逐个存到磁盘文件data.txt中,直到输入’\n’为止,并将建立的文本文件的内容在存入文件的同时,在屏幕上显示出来:
- D盘Program Files 文件夹里文件不显示,没隐藏。怎么才能显示出来?
- 在datagrid里面怎么把一列的显示的内容改变和数据库里不一样?比如:数据库里存的是1和0,而datagrid里某一列显示出来是“完成”或“未完成”。如果在datagrid里面这一列输入“完成”,则在数据库里存的是1。
- 怎么让一个html文件在另一个html文件中适当的位置直接显示出来
- linux命令怎么显示文件某一行或几行内容
- 用shell脚本将当前目录中每个文件的文件名及其内容显示出来
- linux命令显示文件内容行号|linux将内容以行号显示出来
- C源程序:输入当前目录下的文本文件名称,尝试打开文件,逐个字符读取文件内容并显示出来
- 怎么正确显示textarea内容的换行
- Hadoop实验:用java把hdfs系统里的文件内容读取出来,显示到屏幕
- linux命令怎么显示文件某一行或几行内容
- 读取文件中的内容将其显示出来
- 怎么设置GridView的单元格在内容显示超过长度的时候用省略号代替而不是换行当鼠标移上去的时候将影藏的内容提示出来
- linux中怎么用shell显示文件某一行或几行内容
- 从键盘输入若干个字符,逐个存到磁盘文件中,直到输入‘\n’为止,并将建立的文本文件的内容在存入文件的同时,在屏幕上显示出来。。
- 在文件列表中选择文件,并把一个文件的内容显示在TextArea中
- 实现TEXTAREA数据写入数据库后提取出来原样显示在网页TABLE上并自动换行
- 怎么把js获得的list数据加上链接定向显示在HTML中,并且点击对应内容会访问链接内容
- 读取BMP文件并显示出来(C++实现)