溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用JAVA寫(xiě)文本編輯器

發(fā)布時(shí)間:2021-11-16 09:07:38 來(lái)源:億速云 閱讀:132 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容介紹了“如何使用JAVA寫(xiě)文本編輯器”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

再來(lái)分析一下,最后一個(gè)Menu,里面有幾個(gè)按鈕,不知道大家發(fā)現(xiàn)沒(méi)有,有兩個(gè)還是特別簡(jiǎn)單,一個(gè)是新建,一個(gè)是退出。新建我們?cè)賹?shí)例化一下父窗口就可以了,但是這里有bug,關(guān)閉任一子窗口父窗口都會(huì)跟著關(guān)掉。另一個(gè)是退出,直接dispose()就好了。在監(jiān)聽(tīng)器里處理一下:

這里就不需要貼太多上下文代碼了,找到主窗口.java 找到該函數(shù)就可以

@Override
 public void actionPerformed(ActionEvent e) {
  if (e.getSource() == item_about) {
   new about_Window();
  }else if (e.getSource() == item_word_format) {
   new about_Format();
  }else if (e.getSource() == item_new) {
   new test5(); // 選中新建 new一個(gè)新窗口 ,有bug,關(guān)閉任意子窗口父窗口也會(huì)跟著關(guān)閉
  }else if (e.getSource() == item_exit) {
   this.dispose();
  }
 }

在JAVA寫(xiě)文本編輯器(一)我們有分析過(guò),有一個(gè)封裝好的工具JFileChooser可以直接調(diào)用。

其實(shí)消化完超鏈接里的這篇組件介紹,對(duì)于文件的存取已經(jīng)沒(méi)什么問(wèn)題了。接下來(lái)我們添加監(jiān)聽(tīng)器,監(jiān)聽(tīng)器里添加對(duì)應(yīng)的方法:

當(dāng)然要先在類內(nèi)聲明JFileChooser

@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == item_about) {
			new about_Window();
		}else if (e.getSource() == item_word_format) {
			new about_Format();
		}else if (e.getSource() == item_new) {
			new test5(); // 選中新建 new一個(gè)新窗口 ,有bug,關(guān)閉任意子窗口父窗口也會(huì)跟著關(guān)閉
		}else if (e.getSource() == item_exit) {
			this.dispose();
		}else if (e.getSource() == item_open) {
			openFile();
		}else if (e.getSource() == item_save) {
			saveFile();
		}
	}

SaveFile方法:

private void saveFile() {
		File file = null;
		int result ;
		fileChooser = new JFileChooser("C:\\");
		fileChooser.setApproveButtonToolTipText("保存"); // 設(shè)置確認(rèn)按鈕的現(xiàn)實(shí)文本
		fileChooser.setDialogTitle("保存文件"); // 設(shè)置title
		result = fileChooser.showOpenDialog(rootPane); // 設(shè)置Dialog的根View 根布局
		
		//--------------------------------------------------------------------------
		if(result == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile(); // 若點(diǎn)擊了確定按鈕,給file填文件路徑
		}
		
		//--------------------------------------------------------------------------
		/*FileOutputStream fileOutputStream = null; // 文件io類
		if (file != null) {
			try {
				fileOutputStream = new FileOutputStream(file); 
			}catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			
			String content = edit_text_area.getText();
			
			try {
				fileOutputStream.write(content.getBytes());
			}catch (IOException e) {
				e.printStackTrace();
			}finally {
				try {
					if (fileOutputStream!=null) {
						fileOutputStream.close();
					}
				}catch (IOException e) {
					e.printStackTrace();
				}
			}
		}*/
		//---------------這里有嚴(yán)重bug,對(duì)于字符寫(xiě)入文件沒(méi)問(wèn)題,但是在讀取中文字符的時(shí)候會(huì)出現(xiàn)亂碼-----------
		//--------------------------------------------------------------------------
		
		try{
			OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file),"UTF-8"); // 對(duì)字符進(jìn)行編碼轉(zhuǎn)換
			BufferedWriter writer = new BufferedWriter(write);
			String content = edit_text_area.getText();
			writer.write(content);
			writer.close();
		}catch(IOException e) {
			e.printStackTrace();
		}
		
		
	}

OpenFile方法:

/**
	 * 點(diǎn)擊新建按item時(shí) 打開(kāi)JFileChooser對(duì)話框
	 * 并且對(duì)文件讀取進(jìn)行處理
	 */
	private void openFile() {
		File file = null;
		int result ;
		fileChooser = new JFileChooser("C:\\");
		fileChooser.setApproveButtonToolTipText("確定"); // 設(shè)置確認(rèn)按鈕的現(xiàn)實(shí)文本
		fileChooser.setDialogTitle("打開(kāi)文件"); // 設(shè)置title
		result = fileChooser.showOpenDialog(rootPane); // 設(shè)置Dialog的根View 根布局
		
		//--------------------------------------------------------------------------
		if(result == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile(); // 若點(diǎn)擊了確定按鈕,給file填文件路徑
		}
		
		//--------------------------------------------------------------------------
		//--------------------下面對(duì)文件進(jìn)行處理,把內(nèi)容裝到父窗體的textarea中--------------------
		/*FileInputStream fileInputStream = null;
		if (file != null) {
			try { //此處需要注意空指針異常 即沒(méi)有找到文件的時(shí)候需要處理
				fileInputStream = new FileInputStream(file); // 將file文件的數(shù)據(jù)流裝到fileInputStream里
			}catch (FileNotFoundException e) {  // 捕獲到異常 ,需要處理
				e.printStackTrace(); // 將異常實(shí)例化為e 然后在控制臺(tái)Console 打印出錯(cuò)誤的位置和原因
				TipDialog tmpDialog = new TipDialog(this,"錯(cuò)誤文件",true,"文件夾名稱錯(cuò)誤,請(qǐng)重新檢查!");// 此處我們還可以對(duì)一場(chǎng)做一些處理,在這里彈出一個(gè)警示對(duì)話框
				
			}
			
			//讀取文件
			int readbyte ;
			try {
				while ((readbyte = fileInputStream.read())!=-1) { //一段段的讀取文件
					edit_text_area.append(String.valueOf((char)readbyte)); //在editarea 里一行行添加
				}
			}catch (IOException e) { // 處理異常
				e.printStackTrace();
			}finally {
				try {
					if (fileInputStream != null) { //對(duì)fileInputStream 回收
						fileInputStream.close();
					}
				}catch (IOException e) { //拋出異常
					e.printStackTrace();
				}
			}
		}*/
		//---------------這里有嚴(yán)重bug,對(duì)于讀取中文字符會(huì)出現(xiàn)亂碼-------------------------------
		//--------------------------------------------------------------------------
		
		if(file.isFile() && file.exists()) {
			BufferedReader reader = null;
			try {
				InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),"UTF-8");
				reader = new BufferedReader(inputStreamReader);
				
				String readLine = "";
				while ((readLine = reader.readLine()) != null) { // 對(duì)BufferedReader數(shù)據(jù)一行行讀
					//edit_text_area.append(readLine); 這樣寫(xiě)會(huì)出現(xiàn)所有的句子都出現(xiàn)在同一行的情況,所以在每次append的時(shí)候在后面加一個(gè)換行符
					edit_text_area.append(readLine+'\n');  //對(duì)edit_text_area 一行行加
				}
				
				reader.close(); // 關(guān)閉reader
				
			}catch (IOException e) {
				e.printStackTrace();
				//TipDialog tmpDialog = new TipDialog(this,"錯(cuò)誤文件",true,"文件夾名稱錯(cuò)誤,請(qǐng)重新檢查!");
			}
			
			
		}
	}

其實(shí)這里的兩個(gè)方法都很相似,通過(guò)FileChooser可以得到選中的文件的路徑,然后通過(guò)File 把路徑拿到,在執(zhí)行一下常規(guī)的文件讀寫(xiě)操作。注意一定要處理IO操作異常。

如果大家在注意的話可以看到其實(shí)我的IO是有一些操作注釋掉的,注釋掉的部分是按照使用JFileChooser組件寫(xiě)的讀寫(xiě)操作,但是經(jīng)過(guò)測(cè)試,發(fā)現(xiàn)對(duì)于中文字符沒(méi)有編碼,所以存入之后再讀取就是亂碼。所以采用了另外一種寫(xiě)法。

“如何使用JAVA寫(xiě)文本編輯器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI