{"title":"Java MiniShell 实现 - 带有命令历史记录和配置文件","description":"这是一个简单的 Java MiniShell 实现,它支持基本命令,并具有命令历史记录功能。它还能够从配置文件中加载配置信息,以自定义提示符、背景、字体等设置。","keywords":"Java, MiniShell, 命令行解释器, 命令历史记录, 配置文件, 终端","content":"import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class MiniShell {private static final String CONFIG_FILE = "miniShell.conf";private static final String DEFAULT_PROMPT = "# ";private static final String DEFAULT_BACKGROUND = "black";private static final String DEFAULT_FONT = "宋体";private static final int DEFAULT_FONTSIZE = 30;private static final String DEFAULT_FONTCOLOR = "white";private String prompt;private String background;private String font;private int fontSize;private String fontColor;private List commandHistory;public MiniShell() {this.prompt = DEFAULT_PROMPT;this.background = DEFAULT_BACKGROUND;this.font = DEFAULT_FONT;this.fontSize = DEFAULT_FONTSIZE;this.fontColor = DEFAULT_FONTCOLOR;this.commandHistory = new ArrayList<>();}public void loadConfig() {try (BufferedReader reader = new BufferedReader(new FileReader(CONFIG_FILE))) {String line;while ((line = reader.readLine()) != null) {line = line.trim();if (!line.isEmpty() && !line.startsWith("#")) {String[] parts = line.split("=");if (parts.length == 2) {String key = parts[0].trim();String value = parts[1].trim();switch (key) {case "promote":{this.prompt = value;break;}case "background":{this.background = value;break;}case "font":{this.font = value;break;}case "fontsize":{this.fontSize = Integer.parseInt(value);break;}case "fontcolor":{this.fontColor = value;break;}}}}}} catch (IOException e) {// Ignore if config file is not found or cannot be read}}public void start() {Scanner scanner = new Scanner(System.in);while (true) {System.out.print(prompt);String command = scanner.nextLine();commandHistory.add(command);if (command.equals("exit")) {break;}executeCommand(command);}scanner.close();}private void executeCommand(String command) {String[] parts = command.split(" ");String cmd = parts[0];switch (cmd) {case "ls":{// Handle ls commandbreak;}case "cd":{// Handle cd commandbreak;}case "mkdir":{// Handle mkdir commandbreak;}case "rmdir":{// Handle rmdir commandbreak;}case "rm":{// Handle rm commandbreak;}case "touch":{// Handle touch commandbreak;}case "chmod":{// Handle chmod commandbreak;}case "cat":{// Handle cat commandbreak;}case "tar":{// Handle tar commandbreak;}case "history":{printCommandHistory();break;}case "pwd":{// Handle pwd commandbreak;}default:{System.out.println("Unknown command: " + cmd);}}}private void printCommandHistory() {for (int i = 0; i < commandHistory.size(); i++) {System.out.println(i + ": " + commandHistory.get(i));}}public static void main(String[] args) {MiniShell miniShell = new MiniShell();miniShell.loadConfig();miniShell.start();}}还需要什么类和接口来运行内容:除了MiniShell类,还需要一个Config类来保存配置信息。此外,还需要一个Command类来表示每个命令,并提供执行命令的方法。最后,可以实现一个History类来保存命令历史记录。


原文地址: https://www.cveoy.top/t/topic/pBWN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录