/*
* 2011 08 29 java IO操作小练习
* Notepad 山寨记事本
*
×很多人都写过的东西,没记错的话已经是第二次写了吧,没什么技巧,也没什么算法,纯属熟悉程序的练手,一直想写一个完整的,还是先把作业交了再说吧
×一直想把程序尽量规范起来,希望能进步快点
× blog:blog.csdn.net/hack2me
* blog: youngershen.com/blog 需要翻墙
* email:younger.x.shen@gmail.com
*/
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class NotePad extends JFrame{
private JMenu menu = null;
private JMenuBar bar = null;
private JMenuItem open = null;
private JMenuItem save = null;
private JMenuItem saveAs = null;
private JTextArea editArea = null;
private BufferedReader reader = null;
private BufferedWriter writer = null;
private ArrayList<String> dumpString = new ArrayList<String>();
private String fileName = null;
private String buffer = null;
private JFileChooser chooser = new JFileChooser();
private File currentFile = null;
private ArrayList<String> currentText = new ArrayList<String>();
//成员初始化
public NotePad(){
super("NotePad By Younger.shen 20110829");
menu = new JMenu("菜单");
bar = new JMenuBar();
open = new JMenuItem("打开");
save = new JMenuItem("保存");
saveAs = new JMenuItem("另存为");
editArea = new JTextArea();
setDefaultCloseOperation(EXIT_ON_CLOSE);
initUI();
getInit();
}
//事件初始化
private void getInit(){
saveAs.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("click the save button");
chooser.setDialogTitle("保存文件");
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setCurrentDirectory(new File("./"));
int flag = 0;
try{
flag = chooser.showSaveDialog(null);
}catch (HeadlessException ex){
System.out.println("保存文件错误");
}
if(JFileChooser.APPROVE_OPTION == flag){
File temp = chooser.getSelectedFile();
fileName = temp.getName();
dumpToFile();
}
}
});
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//add later
}
});
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
chooser.setDialogTitle("打开文件");
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
int flag = 0;
chooser.setCurrentDirectory(new File("./"));
try{
flag = chooser.showOpenDialog(null);
}catch (HeadlessException ex){
System.out.println("保存文件错误");
}
if(JFileChooser.APPROVE_OPTION == flag){
currentFile = chooser.getSelectedFile();
fileName = currentFile.getName();
ArrayList<String> t = openFile(fileName);
for(int i=0;i<t.size();i++){
editArea.append(t.get(i));
editArea.append("\n");
}
}
}
});
}
//文件打开操作
private ArrayList<String> openFile(String name ){
try{
reader = new BufferedReader(new FileReader(name));
String temp = reader.readLine();
while(temp != null){
currentText.add(temp);
temp = reader.readLine();
}
}catch(IOException e){
System.out.println(e.toString());
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOException e){
System.out.println("IO Error");
}
}
}
return currentText;
}
//UI初始化
private void initUI(){
bar.add(menu);
menu.add(open);
menu.add(save);
menu.add(saveAs);
setJMenuBar(bar);
setLayout(null);
add(editArea);
editArea.setBounds(0,0,400,300);
setVisible(true);
setResizable(false);
setSize(400,300);
add(chooser);
editArea.setEnabled(true);
JScrollPane jScrollPane = null;
JPanel jContentPane = new JPanel();
jScrollPane = new JScrollPane();
jContentPane.setLayout(null);
jContentPane.add(jScrollPane, null);
jScrollPane.setBounds(new Rectangle(0, 2, 400, 300));
jScrollPane.setViewportView(editArea);
setContentPane(jContentPane);
}
//聚集字符串的工具方法
private void gatherString(){
buffer = editArea.getText();
}
//保存成文件
public void dumpToFile(){
gatherString();
try{
writer = new BufferedWriter(new FileWriter(fileName,true));
writer.write(buffer,0,buffer.length());
writer.write("\r");
writer.write("\n");
writer.flush();
}catch(IOException e){
System.out.println("dump error");
}finally{
try{
writer.close();
}catch(IOException e){
System.out.println("IO Error");
}
}
}
//主方法这个不用看了
public static void main(String[] args){
new NotePad();
}
}
今天的作业 java IO练习,一个山寨记事本,没什么技术含量
最新推荐文章于 2023-07-02 09:44:31 发布