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

修改前/修改后

相关推荐
jiayong232 分钟前
Spring 框架完全指南
java·后端·spring
高山上有一只小老虎3 分钟前
小红的正整数计数
java·算法
AnAnCode3 分钟前
【时间轮算法-实战】Java基于Netty的 `HashedWheelTimer`快速搭建时间轮算法系统
java·开发语言·算法·时间轮算法
mpHH9 分钟前
ivorysql 源码分析-双port兼容
数据库·学习·postgresql
liu****13 分钟前
12.C语言内存相关函数
c语言·开发语言·数据结构·c++·算法
北杳同学17 分钟前
前端一些用得上的有意思网站
前端·javascript·vue.js·学习
while(1){yan}19 分钟前
JAVA单例模式
java·单例模式
张3蜂22 分钟前
CSRF Token:网络应用安全的关键防线——深度解析与实战指南
前端·安全·csrf
没有bug.的程序员27 分钟前
Async Profiler:最精准的火焰图工具
java·jvm·spring·对象分配·async profiler
小帅学编程32 分钟前
JVM学习记录
jvm·学习