![JAVA實(shí)驗(yàn)報(bào)告二:異常處理及輸入輸出流技術(shù)實(shí)驗(yàn)_第1頁](http://file4.renrendoc.com/view/45ad8eb4f6a3799458e6ae68075b15cd/45ad8eb4f6a3799458e6ae68075b15cd1.gif)
![JAVA實(shí)驗(yàn)報(bào)告二:異常處理及輸入輸出流技術(shù)實(shí)驗(yàn)_第2頁](http://file4.renrendoc.com/view/45ad8eb4f6a3799458e6ae68075b15cd/45ad8eb4f6a3799458e6ae68075b15cd2.gif)
![JAVA實(shí)驗(yàn)報(bào)告二:異常處理及輸入輸出流技術(shù)實(shí)驗(yàn)_第3頁](http://file4.renrendoc.com/view/45ad8eb4f6a3799458e6ae68075b15cd/45ad8eb4f6a3799458e6ae68075b15cd3.gif)
![JAVA實(shí)驗(yàn)報(bào)告二:異常處理及輸入輸出流技術(shù)實(shí)驗(yàn)_第4頁](http://file4.renrendoc.com/view/45ad8eb4f6a3799458e6ae68075b15cd/45ad8eb4f6a3799458e6ae68075b15cd4.gif)
![JAVA實(shí)驗(yàn)報(bào)告二:異常處理及輸入輸出流技術(shù)實(shí)驗(yàn)_第5頁](http://file4.renrendoc.com/view/45ad8eb4f6a3799458e6ae68075b15cd/45ad8eb4f6a3799458e6ae68075b15cd5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、 實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)項(xiàng)目二 Java異常處理及輸入輸出流技術(shù)實(shí)驗(yàn) 文本編輯器一、實(shí)驗(yàn)?zāi)康睦斫夂驼莆兆止?jié)流的類以及它們的使用特點(diǎn);理解和區(qū)分字節(jié)流和字符流;了解和運(yùn)用字節(jié)流和字符流進(jìn)行文件復(fù)制。二、實(shí)驗(yàn)內(nèi)容設(shè)計(jì)一個(gè)簡單的文本編輯器,可以實(shí)現(xiàn)多文件處理、實(shí)現(xiàn)文件的創(chuàng)建、瀏覽、編輯(允許文件復(fù)制和修改)、對已經(jīng)修改的文件可以進(jìn)行保存或以另存的文件名稱保存。三、實(shí)驗(yàn)原理Java輸入輸出流技術(shù)、AWT/Swing編程技術(shù)、事件處理技術(shù)等技術(shù)原理。四、實(shí)驗(yàn)環(huán)境主CPU 586以上,內(nèi)存1G以上,操作系統(tǒng)Windows XP,eclipse。五、實(shí)驗(yàn)步驟 創(chuàng)建一個(gè)TextEditor 項(xiàng)目 圖 1 TextEdi
2、tor 項(xiàng)目 2.創(chuàng)建一個(gè)TextEditor類 圖 2 TextEditor類 3.補(bǔ)充代碼。并在TextEditor類里輸入相對應(yīng)的代碼:import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; import java.awt.*; import java.io.*; import ; public class TextEditor extends JFrame JDesktopPane desktopPane; /定義菜單組件 JMenuBar menuBar; JMenu fileMenu,he
3、lpMenu;/定義文件菜單、幫助菜單 JMenuItem newItem,openItem,saveItem,othersaveItem,closeItem,exitItem; /定義文件菜單的:新建菜單項(xiàng)、打開菜單項(xiàng)、保存菜單項(xiàng)、另存菜單項(xiàng)、關(guān)閉菜單項(xiàng)、/退出菜單項(xiàng) JMenuItem aboutItem;/定義幫助菜單的:關(guān)于菜單項(xiàng) StringBuffer buffer=new StringBuffer();/定義一個(gè)字符串緩沖區(qū); File currentFile;/當(dāng)前處理的文件; int status;/記錄當(dāng)前狀態(tài)0:新建、1:打開、2:保存、3:另存;4:關(guān)閉; Displa
4、yFrame currentFrame;/當(dāng)前打開的窗口 public TextEditor() super(文本編輯器); Container container=getContentPane(); desktopPane=new JDesktopPane(); container.add(desktopPane); createMenu(); setJMenuBar(menuBar); public void createMenu() /定義菜單條; menuBar=new JMenuBar(); /定義文件菜單 fileMenu=new JMenu(文件); /定義文件菜單的:新建菜單項(xiàng)
5、、打開菜單項(xiàng)、保存菜單項(xiàng)、另存菜單項(xiàng)、關(guān)閉菜單 /退出菜單項(xiàng) /定義新建菜單項(xiàng) newItem=new JMenuItem(新建); newItem.addActionListener(new NewFileListener(); /定義打開菜單項(xiàng) openItem=new JMenuItem(打開); openItem.addActionListener(new OpenFileListener(); saveItem=new JMenuItem(保存); saveItem.addActionListener(new SaveFileListener(); othersaveItem=new
6、 JMenuItem(另存為); othersaveItem.addActionListener(new OtherSaveFileListener(); closeItem=new JMenuItem(關(guān)閉); closeItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(e.getSource()=closeItem) currentFrame.dispose(); ); exitItem=new JMenuItem(退出); exitItem.addAction
7、Listener(new ActionListener() public void actionPerformed(ActionEvent e) System.exit(0); ); /添加菜單項(xiàng)到文件菜單中; fileMenu.add(newItem); fileMenu.add(openItem); fileMenu.addSeparator(); fileMenu.add(saveItem); fileMenu.add(othersaveItem); fileMenu.addSeparator(); fileMenu.add(closeItem); fileMenu.add(exitIt
8、em); /定義幫助菜單 helpMenu=new JMenu(幫助); /定義幫助菜單的:關(guān)于菜單項(xiàng) aboutItem=new JMenuItem(關(guān)于); aboutItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(e.getSource()=aboutItem) JOptionPane.showMessageDialog(null,TextEditor 1.1, 版本,JOptionPane.INFORMATION_MESSAGE); ); /添加菜單項(xiàng)到幫
9、助菜單中; helpMenu.add(aboutItem); /將文件菜單和幫助菜單加入到菜單條中; menuBar.add(fileMenu); menuBar.add(helpMenu); /end createMenu method; public File chooseFile(int chooseMode) /從選擇文件對話框中選擇文件,chooseMode為選擇模式 /1:為文件打開對話框模式,2:為文件保存對話框模式; JFileChooser filechooser=new JFileChooser(); filechooser.setFileSelectionMode(JFi
10、leChooser.FILES_ONLY); int result=0; switch(chooseMode) case 1: result =filechooser.showOpenDialog(currentFrame); /代碼1; /按照文件打開對話框模式選擇文件 break; case 2: result=filechooser.showSaveDialog(currentFrame); /代碼2; /按照文件保存對話框模式選擇文件 break; if(result=filechooser.CANCEL_OPTION) return null; File fileName=filec
11、hooser.getSelectedFile(); /代碼3;/從對話框中選擇文件,賦值給fileName; return fileName;/返回選定的文件; public void saveFile(File file,int saveMode) throws IOException /保存文件fileName,saveMode是保存文件模式: /1:新建文件的保存;2:將修改的已有文件保存; File fileName=null; switch(saveMode) case 0:fileName=chooseFile(2); if(fileName.createNewFile()!=tr
12、ue) return; break; case 1:fileName=file;break; default:return; currentFile=fileName; if(fileName.exists()!=true) return;/文件不存在,則返回; BufferedWriter input=new BufferedWriter(new FileWriter (fileName); /代碼4; /將文件fileName寫入到字符緩沖寫入器input中; input.write(buffer.toString(),0,buffer.length()-1); /將buffer字符串寫入
13、到字符緩沖寫入器input中 System.err.println(buffer.length(); if(input!=null) input.close(); status=1; public void showFile(File fileName,DisplayFrame contentFrame) /將指定文件fileName的內(nèi)容在窗口contentFrame內(nèi)顯示出來。 try BufferedReader output=new BufferedReader(new FileReader (fileName); /代碼5; /將文件fileName讀到字符緩沖讀取器output中;
14、 String text; buffer=new StringBuffer(); while(text=output.readLine()!=null)/依次訪問文件的每一行; / 代碼6; buffer.append(text+n); /將每一行添加到buffer中; contentFrame.displayArea.setText(buffer.toString(); /將buffer的字符串在contentFrame窗口中顯示出來; contentFrame.setTitle(fileName.getName();/改變窗口的標(biāo)題; output.close(); / 代碼7;/關(guān)閉字符
15、緩沖流output; catch(IOException ex) JOptionPane.showMessageDialog(null,錯(cuò)誤發(fā)生!); catch(NullPointerException ex3) JOptionPane.showMessageDialog(null,警告!); public static void main(String args) TextEditor texteditor = new TextEditor(); texteditor.setSize(400,300); texteditor.setVisible(true); texteditor.set
16、DefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /end main class NewFileListener implements ActionListener public void actionPerformed(ActionEvent e)/新建 DisplayFrame txtFrame=new DisplayFrame(); currentFrame=txtFrame; txtFrame.setTitle(新建文件); desktopPane.add(txtFrame); status=0; /end class newFileListen
17、er; class OpenFileListener implements ActionListener public void actionPerformed(ActionEvent e)/打開文件監(jiān)視接口; File fileName=chooseFile(1);/打開模式打開文件; currentFile=fileName; try if(fileName.exists() DisplayFrame contentFrame=new DisplayFrame(); currentFrame=contentFrame; desktopPane.add(contentFrame); show
18、File(fileName,contentFrame); /end if status=1; catch(Exception ex) /end actionPerformed /end class openFileListener; class SaveFileListener implements ActionListener public void actionPerformed(ActionEvent e)/保存處理 if (e.getSource() = saveItem) try saveFile(currentFile, status); catch (IOException ex
19、) JOptionPane.showMessageDialog(null, 保存文件失?。?; catch(Exception ex) /end class SaveFileListener; class OtherSaveFileListener implements ActionListener public void actionPerformed(ActionEvent e)/另存處理; if(e.getSource()=othersaveItem) File fileName=chooseFile(2); BufferedWriter output=null; BufferedRea
20、der input=null; try if(fileName.exists()!=true) fileName.createNewFile(); input = new BufferedReader(new FileReader(currentFile); output = new BufferedWriter(new FileWriter(fileName); String ch; while (ch=input.readLine()!=null) output.write(ch); output.newLine(); input.close(); output.close(); curr
21、entFrame.dispose();/將源文件顯示窗口關(guān)閉; DisplayFrame contentFrame=new DisplayFrame(); desktopPane.add(contentFrame);/創(chuàng)建新的顯示窗口; currentFrame=contentFrame;/新窗口設(shè)置為當(dāng)前顯示窗口; currentFile=fileName; showFile(fileName,contentFrame); /在窗口contentFrame中顯示文件fileName; catch(IOException ex) JOptionPane.showMessageDialog(nu
22、ll,文件另存失?。?; /end outer if /end actionPerformed method; /end class OtherSaveListener; class DisplayFrame extends JInternalFrame implements DocumentListener JTextArea displayArea; public DisplayFrame() super(文本內(nèi)容:, true, true, true, true); displayArea = new JTextArea(8, 30); displayArea.getDocument()
23、.addDocumentListener(this); getContentPane().add(displayArea); pack(); setVisible(true); public void insertUpdate(DocumentEvent e) buffer=new StringBuffer(displayArea.getText(); displayArea.setCaretPosition(displayArea.getDocument().getLength(); public void removeUpdate(DocumentEvent e) buffer=new S
24、tringBuffer(displayArea.getText(); displayArea.setCaretPosition(displayArea.getDocument().getLength(); public void changedUpdate(DocumentEvent e) /Plain text components dont fire these events. /end DisplayFrame.java /end class TextEditor.java 六、實(shí)驗(yàn)結(jié)果 運(yùn)行可得如下實(shí)驗(yàn)結(jié)果: 圖 3 結(jié)果1 圖 4 新建文件 圖 5 打開文件 圖 6 保存文件 圖 7
25、 文件另存為 圖 8 幫助文件實(shí)驗(yàn)分析7.1 程序中的類 SaveFileListener 和類 OtherSaveFileListener 的定義 。 它們都有處理保存文件數(shù)據(jù)的功能 , 問在具體實(shí)現(xiàn)上 , SaveFileListener 如果對已經(jīng)保存的文件它,會直接保存;但OtherSaveFileListener每次都會重裝打開一個(gè)窗口保存文件,并要在對話框里輸入文件名。7.2 在 OtherSaveFileListener 類中的 actionPerformed() 方法中有語句 : output = new BufferedWriter(new FileWriter(fileNa
26、me); 如果將該語句改寫成: output = new BufferedWriter(new FileWriter(filename, true); 程序是否可以正常運(yùn)行 , 如果不能正常運(yùn)行 ,但改為:output = new BufferedWriter(new FileWriter(fileName, true);可以正常運(yùn)行,文件名錯(cuò)了。new FileWriter(fileName)在給出文件名的情況下構(gòu)造一個(gè) FileWriter 對象。new FileWriter(fileName, true)在給出文件名的情況下構(gòu)造 FileWriter 對象,它具有指示是否掛起寫入數(shù)據(jù)的 boolean 值。7.3 在定義 OtherSaveFileListener 類的 actionPerformed() 方法中 , 采用了字符緩沖讀寫器 (BufferedReader 和 BufferedWriter), 來實(shí)現(xiàn)文件數(shù)據(jù)的讀取和寫
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 現(xiàn)代企業(yè)如何通過公關(guān)活動(dòng)吸引目標(biāo)客戶
- 理論與實(shí)踐在文化傳承中尋求創(chuàng)新發(fā)展
- 國慶節(jié)鮮奶活動(dòng)方案策劃
- Module5 Unit1 He is playing the suona,but the phone rings(說課稿)-2023-2024學(xué)年外研版(三起)英語六年級下冊
- 8《上課了》說課稿-2023-2024學(xué)年道德與法治一年級上冊統(tǒng)編版001
- 2023九年級數(shù)學(xué)上冊 第23章 圖形的相似23.4 中位線說課稿 (新版)華東師大版
- 9 知法守法 依法維權(quán) 說課稿 -2023-2024學(xué)年道德與法治六年級上冊(統(tǒng)編版)
- 2024年四年級英語上冊 Module 4 The world around us Unit 11 Shapes說課稿 牛津滬教版(三起)
- Unit8 I can do this for you 第三課時(shí)(說課稿)-2024-2025學(xué)年譯林版(三起)(2024)英語三年級上冊
- 3 光的傳播會遇到阻礙嗎 說課稿-2024-2025學(xué)年科學(xué)五年級上冊教科版
- GB 12710-2024焦化安全規(guī)范
- 青島版二年級數(shù)學(xué)下冊(六三制)全冊課件【完整版】
- 馬蹄焰玻璃窯爐設(shè)計(jì)技術(shù)培訓(xùn)-課件
- 2023年主治醫(yī)師(中級)-眼科學(xué)(中級)代碼:334考試歷年真題集錦附答案
- 電力安全工作規(guī)程-(電網(wǎng)建設(shè)部分)
- 新加坡小學(xué)二年級英語試卷practice 2
- 小學(xué)五年級英語20篇英文閱讀理解(答案附在最后)
- 2023年遼寧鐵道職業(yè)技術(shù)學(xué)院高職單招(英語)試題庫含答案解析
- GB/T 23800-2009有機(jī)熱載體熱穩(wěn)定性測定法
- T-SFSF 000012-2021 食品生產(chǎn)企業(yè)有害生物風(fēng)險(xiǎn)管理指南
- 水庫工程施工組織設(shè)計(jì)
評論
0/150
提交評論