Java语言程序设计基础篇_编程练习题**17.21 (十六进制编辑器)

目录

题目:**17.21 (十六进制编辑器)

代码示例

结果展示


题目:**17.21 (十六进制编辑器)

编写一个 GUI 应用程序,让用户在文本域输入一个文件名,然后按回车键,在文本域显示它的十六进制表达形式。用户也可以修改十六进制代码,然后将它回存到这个文件中,如图17-23b所示。

代码示例

编程练习题17_21HexEditor.java

java 复制代码
package chapter_17;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class 编程练习题17_21HexEditor extends Application{
	private TextField tfInput;
	private TextArea textArea;
	private String FilePath;
	@Override
	public void start(Stage primaryStage) throws Exception {
		VBox vBox = getPane();
		
		tfInput.setOnKeyPressed(e ->{
			if(e.getCode() == KeyCode.ENTER) {
				try{
					readFile();
				}catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		});
		
		Scene scene = new Scene(vBox);
		primaryStage.setTitle("编程练习题17_21HexEditor");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	public VBox getPane() {
		VBox vBox = new VBox();
		
		tfInput = new TextField();
		tfInput.setPrefWidth(300);
		Label lbInput = new Label("Enter a file:",tfInput);
		lbInput.setContentDisplay(ContentDisplay.RIGHT);
		
		textArea = new TextArea();
		Button btSave = new Button("Save the change");
		btSave.setOnAction(e ->{
			try {
				saveFile();
			}catch (IOException ex) {
				ex.printStackTrace();
			}
		});
		
		vBox.setAlignment(Pos.CENTER);
		vBox.getChildren().addAll(lbInput,tfInput,textArea,btSave);
		return vBox;
	}
	public void readFile() throws IOException{
		FilePath = tfInput.getText().replaceAll("\\\\", "/");
		if(!FilePath.isEmpty()) {
			try(
				FileInputStream input = new FileInputStream(FilePath);
				){
				int read;
				while((read = input.read()) != -1) {
					 if (read == '\n') { // 仅检查 \n  
						 textArea.appendText("\n");
					 }else
						 textArea.appendText(getHex(read)+" ");
				}
				
			}
		}
	}
	public void saveFile() throws IOException {  
	    if (!FilePath.isEmpty()) {  
	        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FilePath))) {  
	            String text = textArea.getText();  
	            String[] s = text.split(" ");
	            for(String str:s) {
	            	if(str.contains("\n")) {
	            		writer.write("\n");
	            	}
	            	int i = hexStringToDecimal(str);
	            	writer.write((char)i);
	            }
	        }  
	    }  
	}
	public static int hexStringToDecimal(String hex) {  
		return Integer.parseInt(hex,16);
	}
	public static String getHex(int value) {
		return Integer.toHexString(value);
	}
	public static void main(String[] args) {
		Application.launch(args);
	}
}
结果展示

C:\Users\Lenovo\eclipse-workspace\JavaFX\src\Text\Exercise17_21.txt

修改前/修改后

相关推荐
生物小卡拉1 天前
R脚本--表达矩阵与特征矩阵相关性分析
笔记·学习·机器学习
liliangcsdn1 天前
从LLM角度学习和了解MoE架构
人工智能·学习·transformer
能不能别报错1 天前
K8s学习笔记(十四) DaemonSet
笔记·学习·kubernetes
报错小能手1 天前
linux学习笔记(19)进程间通讯——消息队列
linux·笔记·学习
进击的圆儿1 天前
【学习笔记05】C++11新特性学习总结(下)
c++·笔记·学习
dlraba8021 天前
用 Python+OpenCV 实现实时文档扫描:从摄像头捕捉到透视矫正全流程
开发语言·python·opencv
芒果茶叶1 天前
并行SSR,SSR并行加载
前端·javascript·架构
Haooog1 天前
98.验证二叉搜索树(二叉树算法题)
java·数据结构·算法·leetcode·二叉树
武子康1 天前
Java-143 深入浅出 MongoDB NoSQL:MongoDB、Redis、HBase、Neo4j应用场景与对比
java·数据库·redis·mongodb·性能优化·nosql·hbase
vortex51 天前
解决 Kali 中 Firefox 下载语言包和插件速度慢的问题:配置国内镜像加速
前端·firefox·腾讯云