国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

教你用Java GUI實(shí)現(xiàn)文本文件的讀寫

瀏覽:101日期:2022-08-12 09:27:17
目錄一、實(shí)驗(yàn)題目二、分析三、實(shí)現(xiàn)四、全部代碼一、實(shí)驗(yàn)題目

教你用Java GUI實(shí)現(xiàn)文本文件的讀寫

二、分析

實(shí)驗(yàn)要求為:

實(shí)現(xiàn)一個(gè)界面,界面中包含一個(gè)文本顯示區(qū)和兩個(gè)按鈕(存檔和讀檔) 讀檔按鈕作用是打開文件并讀取內(nèi)容,將內(nèi)容顯示在文本區(qū)中 存檔按鈕作用是將文本區(qū)的內(nèi)容寫入到文件中。

簡單分析一下,可以看出這樣的要求奧,包含的要考察知識(shí)點(diǎn)主要有兩個(gè)方向:

GUI繪制界面并添加事件 使用IO流對(duì)象對(duì)文件進(jìn)行讀寫

好的小伙伴們,廢話不多說,下面就來的實(shí)現(xiàn)它。

三、實(shí)現(xiàn)

首先,讓我們創(chuàng)建一個(gè)GUI界面,先秉持著一切從簡的設(shè)計(jì)思想,預(yù)計(jì)它長這樣:

教你用Java GUI實(shí)現(xiàn)文本文件的讀寫

這樣的布局方式,我們可以選擇采用流布局實(shí)現(xiàn),在容器中直接放入文本顯示區(qū)和兩個(gè)按鈕,適當(dāng)調(diào)整窗口大小即可實(shí)現(xiàn):

import java.awt.Container;import java.awt.FlowLayout;import java.awt.TextArea;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;public class GUIDemo extends JFrame{//三個(gè)組件private JButton saveButton;private JButton loadButton;private TextArea textArea;//容器private Container container;public GUIDemo() {//設(shè)置titlesuper('File Demo');//設(shè)置流布局setLayout(new FlowLayout());//獲取容器container = getContentPane();//三個(gè)組件textArea = new TextArea();saveButton = new JButton('save');loadButton = new JButton('load');//保存文件按鈕點(diǎn)擊事件saveButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println('存檔成功');}});//讀入文件按鈕點(diǎn)擊事件loadButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println('讀檔成功');}});//裝填三個(gè)組件container.add(textArea);container.add(loadButton);container.add(saveButton);//調(diào)整大小setSize(500, 300);//顯示setVisible(true);}public static void main(String[] args) {GUIDemo demo = new GUIDemo();demo.setDefaultCloseOperation(EXIT_ON_CLOSE);}}

代碼的含義都在注釋里面,就不??陸步飭恕?/p>

跑起來是這個(gè)樣子:

教你用Java GUI實(shí)現(xiàn)文本文件的讀寫

點(diǎn)擊兩下按鈕測(cè)試點(diǎn)擊事件,控制臺(tái)輸出:

教你用Java GUI實(shí)現(xiàn)文本文件的讀寫

好的,GUI界面設(shè)計(jì)完畢,下面來為兩個(gè)按鈕編寫點(diǎn)擊事件。

首先要解決的一個(gè)問題是“目標(biāo)文件”。由于題目中沒有提到目標(biāo)文件是否需要從文件系統(tǒng)中選取產(chǎn)生,那么我們不妨?xí)簳r(shí)將目標(biāo)文件地址直接在代碼中,令private static final String TARGET_FILE= './temp.txt';

教你用Java GUI實(shí)現(xiàn)文本文件的讀寫

那么在初始化頁面時(shí)就應(yīng)該先創(chuàng)建這個(gè)文件路徑對(duì)應(yīng)的file對(duì)象:

//目標(biāo)文件private File targetFile;...//創(chuàng)建目標(biāo)文件對(duì)象targetFile = new File(TARGET_FILE);if(targetFile.createNewFile()) {System.out.println('文件不存在,創(chuàng)建成功');}else {System.out.println('文件存在');}

這里需要注意幾個(gè)問題:

1.創(chuàng)建目標(biāo)文件需要使用createNewFile()方法,而非mkdir()方法。否則會(huì)創(chuàng)建成為文件夾而非文件

2.createNewFile()方法會(huì)拋出一個(gè)IOException,為了便于處理,這里直接選擇將異常從構(gòu)造方法和主方法中拋出;

教你用Java GUI實(shí)現(xiàn)文本文件的讀寫教你用Java GUI實(shí)現(xiàn)文本文件的讀寫

教你用Java GUI實(shí)現(xiàn)文本文件的讀寫

教你用Java GUI實(shí)現(xiàn)文本文件的讀寫

處理好目標(biāo)文件問題,兩次啟動(dòng)程序,可以看到控制臺(tái)輸出:

教你用Java GUI實(shí)現(xiàn)文本文件的讀寫教你用Java GUI實(shí)現(xiàn)文本文件的讀寫

哦吼,文件處理成功。

接著,就是在為兩個(gè)按鈕添加點(diǎn)擊事件。在下面的處理中,對(duì)于IO流的選擇,我們統(tǒng)一選擇字符流.

首先是讀檔按鈕,它的點(diǎn)擊事件邏輯大致為:

1.創(chuàng)建目標(biāo)文件的輸入字符流

2.從輸入流中讀取文件中的內(nèi)容并形成結(jié)果

3.關(guān)閉輸入流

4.將讀入的結(jié)果顯示在文本顯示區(qū)中

實(shí)現(xiàn)成為代碼:

//讀入文件按鈕點(diǎn)擊事件loadButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {//字符讀入流FileReader reader = new FileReader(targetFile);//讀入緩沖區(qū)char[] buffer = new char[1024];//讀入結(jié)果StringBuffer result = new StringBuffer();//每次讀入緩沖區(qū)的長度int len;//從讀入流中讀取文件內(nèi)容并形成結(jié)果while((len = reader.read(buffer)) != -1) {result.append(buffer,0,len);}//關(guān)閉讀入流reader.close();//更新文本顯示區(qū)內(nèi)容textArea.setText(result.toString());System.out.println('讀檔成功');} catch (FileNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}});

在目標(biāo)文件中寫下Hello World!!,運(yùn)行程序,點(diǎn)擊load:

教你用Java GUI實(shí)現(xiàn)文本文件的讀寫

nice~~

好的,接下來就剩下最后一項(xiàng)任務(wù)了,完成存檔!

存檔按鈕的點(diǎn)擊事件應(yīng)該為:

1.打開目標(biāo)文件字符輸出流

2.獲取當(dāng)前文本顯示區(qū)的內(nèi)容

3.將文本顯示區(qū)的內(nèi)容通過輸出流寫入文件

4.關(guān)閉輸出流

5.清空文本顯示區(qū)

哦吼,最后一條是我加上去的,其實(shí)不清空也可以。

代碼實(shí)現(xiàn)如下:

//保存文件按鈕點(diǎn)擊事件saveButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {//打開文件字符輸出流FileWriter writer = new FileWriter(targetFile);//獲取文本顯示區(qū)文本String result = textArea.getText();//寫入文件writer.write(result);//關(guān)閉輸出流writer.close();//清空文本顯示區(qū)內(nèi)容textArea.setText('');System.out.println('存檔成功');} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}});

在文本顯示區(qū)中輸入Hello Java!!,點(diǎn)擊save:

教你用Java GUI實(shí)現(xiàn)文本文件的讀寫

啥?你說文本框里面啥也沒有?對(duì),因?yàn)樽詈蟀褍?nèi)容清空了!

四、全部代碼

好了,實(shí)現(xiàn)了上面的全部功能,最后把代碼匯總在這里:

(謹(jǐn)慎抄襲哦)

import java.awt.Container;import java.awt.FlowLayout;import java.awt.TextArea;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import javax.swing.JButton;import javax.swing.JFrame;public class GUIDemo extends JFrame{private static final String TARGET_FILE = './temp.txt';//三個(gè)組件private JButton saveButton;private JButton loadButton;private TextArea textArea;//容器private Container container;//目標(biāo)文件private File targetFile;public GUIDemo() throws IOException {//設(shè)置titlesuper('File Demo');//設(shè)置流布局setLayout(new FlowLayout());//獲取容器container = getContentPane();//創(chuàng)建目標(biāo)文件對(duì)象targetFile = new File(TARGET_FILE);if(targetFile.createNewFile()) {System.out.println('文件不存在,創(chuàng)建成功');}else {System.out.println('文件存在');}//三個(gè)組件textArea = new TextArea();saveButton = new JButton('save');loadButton = new JButton('load');//保存文件按鈕點(diǎn)擊事件saveButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {//打開文件字符輸出流FileWriter writer = new FileWriter(targetFile);//獲取文本顯示區(qū)文本String result = textArea.getText();//寫入文件writer.write(result);//關(guān)閉輸出流writer.close();//清空文本顯示區(qū)內(nèi)容textArea.setText('');System.out.println('存檔成功');} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}});//讀入文件按鈕點(diǎn)擊事件loadButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {//字符讀入流FileReader reader = new FileReader(targetFile);//讀入緩沖區(qū)char[] buffer = new char[1024];//讀入結(jié)果StringBuffer result = new StringBuffer();//每次讀入緩沖區(qū)的長度int len;//從讀入流中讀取文件內(nèi)容并形成結(jié)果while((len = reader.read(buffer)) != -1) {result.append(buffer,0,len);}//關(guān)閉讀入流reader.close();//更新文本顯示區(qū)內(nèi)容textArea.setText(result.toString());System.out.println('讀檔成功');} catch (FileNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}});//裝填三個(gè)組件container.add(textArea);container.add(loadButton);container.add(saveButton);//調(diào)整大小setSize(500, 300);//顯示setVisible(true);}public static void main(String[] args) throws IOException {GUIDemo demo = new GUIDemo();demo.setDefaultCloseOperation(EXIT_ON_CLOSE);}}

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 石楼县| 利津县| 克东县| 丘北县| 普洱| 江陵县| 连平县| 丰原市| 临城县| 从江县| 威海市| 平谷区| 长寿区| 文水县| 香港 | 清新县| 紫阳县| 正安县| 辉南县| 西和县| 大化| 隆化县| 射阳县| 那坡县| 岳池县| 宜川县| 右玉县| 高邮市| 民乐县| 墨玉县| 泾阳县| 石棉县| 如东县| 察哈| 修水县| 瑞丽市| 塔河县| 惠水县| 屏边| 宁陕县| 伽师县|