Java流与文件操作汇总.docx
《Java流与文件操作汇总.docx》由会员分享,可在线阅读,更多相关《Java流与文件操作汇总.docx(18页珍藏版)》请在冰豆网上搜索。
![Java流与文件操作汇总.docx](https://file1.bdocx.com/fileroot1/2023-6/2/93681dda-84db-4285-b3d7-e9869c0ef8b6/93681dda-84db-4285-b3d7-e9869c0ef8b61.gif)
Java流与文件操作汇总
实验报告
课程名称
面向对象程序设计
实验名称
实验八流与文件操作
日期
2016-11-18
学生学号
姓名
班级
实验目的:
通过本次实验,要求学生能进一步掌握流与文件的操作方法。
实验条件:
电脑一台、能上网查阅资料。
1.在课堂代码的基础上,理解相关的文件操作部分的代码,进一步完善记事本的设计。
源代码:
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.EventQueue;
importjavax.swing.JColorChooser;
importjavax.swing.JFileChooser;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.border.EmptyBorder;
importjavax.swing.event.DocumentEvent;
importjavax.swing.event.DocumentListener;
importjavax.swing.filechooser.FileFilter;
importjavax.swing.JTextArea;
importjavax.swing.JMenuBar;
importjavax.swing.JMenu;
importjavax.swing.JMenuItem;
importjavax.swing.JOptionPane;
importjavax.swing.JScrollPane;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.awt.print.PrinterJob;
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.awt.event.ActionListener;
importjava.awt.event.ActionEvent;
importjavax.swing.JPopupMenu;
importjava.awt.Component;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjavax.swing.KeyStroke;
importjava.awt.event.KeyEvent;
importjava.awt.event.InputEvent;
importjavax.swing.JToolBar;
importjavax.swing.JLabel;
importjava.awt.event.KeyAdapter;
importjavax.swing.JButton;
importjavax.swing.ImageIcon;
publicclassNotePadPlusextendsJFrame{
privateJPanelcontentPane;
privateJTextAreatxtEditor;
privateStringfilePath;
privateStringfileName;
privateJLabellblMsg=null;
//自动换行和状态栏
booleanflag=false;
/**
*Launchtheapplication.
*/
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
NotePadPlusframe=newNotePadPlus();
frame.setVisible(true);
}catch(Exceptione){
e.printStackTrace();
}
}
});
}
/**
*Createtheframe.
*/
publicNotePadPlus(){
addWindowListener(newWindowAdapter(){
@Override
publicvoidwindowClosing(WindowEvente){
frmMainWindowClosing(e);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,450,300);
JMenuBarmenuBar=newJMenuBar();
setJMenuBar(menuBar);
JMenumenu=newJMenu("\u6587\u4EF6");
menuBar.add(menu);
JMenuItemmiOpenFile=newJMenuItem("\u6253\u5F00\u6587\u4EF6");
miOpenFile.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
openFileActionPerformed(e);
}
});
miOpenFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
menu.add(miOpenFile);
JMenuItemmiSaveFile=newJMenuItem("\u4FDD\u5B58\u6587\u4EF6");
miSaveFile.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
//保存
saveFile();
}
});
miSaveFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
menu.add(miSaveFile);
JMenuItemmiSaveAs=newJMenuItem("\u53E6\u5B58\u4E3A...");
miSaveAs.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
saveAsActionPerformed(e);
}
});
miSaveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK|InputEvent.ALT_MASK));
menu.add(miSaveAs);
JMenuItemmiExit=newJMenuItem("\u9000\u51FA");
miExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
miExit.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
notePadPlusClosing();
}
});
menu.add(miExit);
JMenumenu_1=newJMenu("\u7F16\u8F91");
menuBar.add(menu_1);
JMenuItemmiColor=newJMenuItem("\u989C\u8272");
miColor.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
colorAddActionPerformed(e);
}
});
miColor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK|InputEvent.ALT_MASK));
menu_1.add(miColor);
JMenuItemmiCopy=newJMenuItem("\u62F7\u8D1D");
miCopy.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
txtEditor.copy();
}
});
miCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
menu_1.add(miCopy);
JMenuItemmiCut=newJMenuItem("\u526A\u5207");
miCut.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
txtEditor.cut();
}
});
miCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
menu_1.add(miCut);
JMenuItemmiPaste=newJMenuItem("\u7C98\u8D34");
miPaste.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
txtEditor.paste();
}
});
miPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
menu_1.add(miPaste);
contentPane=newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));
contentPane.setLayout(newBorderLayout(0,0));
setContentPane(contentPane);
JScrollPanescrollPane=newJScrollPane();
contentPane.add(scrollPane,BorderLayout.CENTER);
txtEditor=newJTextArea();
txtEditor.addKeyListener(newKeyAdapter(){
@Override
publicvoidkeyPressed(KeyEvente){
editorKeyPressed(e);
}
});
//文件改变事件
txtEditor.getDocument().addDocumentListener(newDocumentListener(){
publicvoidchangedUpdate(DocumentEvente){
flag=true;
}
publicvoidinsertUpdate(DocumentEvente){
flag=true;
}
publicvoidremoveUpdate(DocumentEvente){
flag=true;
}
});
scrollPane.setViewportView(txtEditor);
JPopupMenupopupMenu=newJPopupMenu();
addPopup(txtEditor,popupMenu);
JMenuItempmiCopy=newJMenuItem("\u62F7\u8D1D");
pmiCopy.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
txtEditor.copy();
}
});
pmiCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
popupMenu.add(pmiCopy);
JMenuItempmiCut=newJMenuItem("\u526A\u5207");
pmiCut.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
txtEditor.cut();
}
});
pmiCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
popupMenu.add(pmiCut);
JMenuItempmiPaste=newJMenuItem("\u7C98\u8D34");
pmiPaste.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
txtEditor.paste();
}
});
pmiPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
popupMenu.add(pmiPaste);
JToolBartb_tools=newJToolBar();
scrollPane.setColumnHeaderView(tb_tools);
JButtonbtnSave=newJButton("");
btnSave.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
saveFile();
}
});
btnSave.setIcon(newImageIcon(NotePadPlus.class.getResource("/images/save.png")));
tb_tools.add(btnSave);
JButtonbtnCopy=newJButton("");
btnCopy.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
txtEditor.copy();
}
});
btnCopy.setIcon(newImageIcon(NotePadPlus.class.getResource("/images/copy.png")));
tb_tools.add(btnCopy);
JButtonbtnCut=newJButton("");
btnCut.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
txtEditor.cut();
}
});
btnCut.setIcon(newImageIcon(NotePadPlus.class.getResource("/images/cut.png")));
tb_tools.add(btnCut);
JButtonbtnPaste=newJButton("");
btnPaste.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
txtEditor.paste();
}
});
btnPaste.setIcon(newImageIcon(NotePadPlus.class.getResource("/images/paste.png")));
tb_tools.add(btnPaste);
JToolBartb_status=newJToolBar();
contentPane.add(tb_status,BorderLayout.SOUTH);
lblMsg=newJLabel("\u5F53\u524D\u5B57\u6570:
0\u5F53\u524D\u884C\u6570:
0");
tb_status.add(lblMsg);
if(fileName==null||fileName==""){
this.setTitle("Untitled.txt");
}else{
this.setTitle("fileName");
}
this.setLocationRelativeTo(null);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
protectedvoideditorKeyPressed(KeyEvente){
//TODOAuto-generatedmethodstub
lblMsg.setText("当前字数:
"+String.valueOf(txtEditor.getText().trim().length())+""+"当前行数:
"
+String.valueOf(txtEditor.getLineCount()));
}
/**
*另存为
*
*@parame
*/
protectedvoidsaveAsActionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
JFileChooserdlg=newJFileChooser();
dlg.setFileFilter(newTxtFileFilter(".txt","文本文件(*.txt)"));
intresult=dlg.showSaveDialog(this);//打开"另存为文件"对话框
if(result==JFileChooser.APPROVE_OPTION){
Filefile=dlg.getSelectedFile();
//设置编辑器标题显示成文件名
this.setTitle(file.getName());
this.filePath=file.getPath();
this.fileName=file.getName();
//把文件的内容读到文本框
FileWriterfw;
try{
fw=newFileWriter(file);
BufferedWriterbw=newBufferedWriter(fw);
//把文本框的内容全部写到文件
fw.write(txtEditor.getText());
bw.close();
fw.close();
}catch(FileNotFoundExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}catch(IOExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
}
}
/**
*打开文件操作
*
*@parame
*/
protectedvoidopenFileActionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
JFileChooserdlg=newJFileChooser();
TxtFileFiltertxtFileFilter=newTxtFileFilter(".txt","文本文件(*.txt)")