用Eclipse编写Linux终端模拟器:支持常用命令和历史记录
要编写一个类似于Ubuntu终端的程序,可以使用Java编写一个简单的控制台应用程序。以下是一个示例程序,满足您提到的要求:\n\njava\nimport java.io.File;\nimport java.io.IOException;\nimport java.nio.file.Files;\nimport java.nio.file.Path;\nimport java.nio.file.StandardCopyOption;\nimport java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.List;\nimport java.util.Scanner;\n\npublic class Terminal {\n private static List<String> commandHistory = new ArrayList<>();\n \n public static void main(String[] args) {\n try (Scanner scanner = new Scanner(System.in)) {\n String command;\n do {\n System.out.print("> ");\n command = scanner.nextLine();\n commandHistory.add(command);\n executeCommand(command);\n } while (!command.equalsIgnoreCase("exit"));\n }\n }\n \n private static void executeCommand(String command) {\n String[] tokens = command.trim().split("\s+");\n String commandName = tokens[0];\n String[] arguments = Arrays.copyOfRange(tokens, 1, tokens.length);\n \n switch (commandName) {\n case "ls":\n listFiles(arguments);\n break;\n case "cd":\n changeDirectory(arguments);\n break;\n case "mkdir":\n createDirectory(arguments);\n break;\n case "rmdir":\n removeDirectory(arguments);\n break;\n case "rm":\n removeFile(arguments);\n break;\n case "touch":\n createFile(arguments);\n break;\n case "chmod":\n changeFilePermissions(arguments);\n break;\n case "cat":\n printFileContent(arguments);\n break;\n case "tar":\n handleTarCommand(arguments);\n break;\n case "history":\n printCommandHistory();\n break;\n case "pwd":\n printWorkingDirectory();\n break;\n default:\n System.out.println("Unknown command: " + commandName);\n break;\n }\n }\n \n private static void listFiles(String[] arguments) {\n File directory;\n if (arguments.length > 0) {\n directory = new File(arguments[0]);\n } else {\n directory = new File(System.getProperty("user.dir"));\n }\n \n if (!directory.exists() || !directory.isDirectory()) {\n System.out.println("Directory not found: " + directory.getAbsolutePath());\n return;\n }\n \n File[] files = directory.listFiles();\n if (files != null) {\n for (File file : files) {\n System.out.println(file.getName());\n }\n }\n }\n \n private static void changeDirectory(String[] arguments) {\n if (arguments.length == 0) {\n System.out.println("Missing directory argument.");\n return;\n }\n \n File directory = new File(arguments[0]);\n if (!directory.exists() || !directory.isDirectory()) {\n System.out.println("Directory not found: " + directory.getAbsolutePath());\n return;\n }\n \n System.setProperty("user.dir", directory.getAbsolutePath());\n }\n \n private static void createDirectory(String[] arguments) {\n if (arguments.length == 0) {\n System.out.println("Missing directory argument.");\n return;\n }\n \n File directory = new File(arguments[0]);\n if (directory.exists()) {\n System.out.println("Directory already exists: " + directory.getAbsolutePath());\n return;\n }\n \n if (directory.mkdir()) {\n System.out.println("Directory created: " + directory.getAbsolutePath());\n } else {\n System.out.println("Failed to create directory: " + directory.getAbsolutePath());\n }\n }\n \n private static void removeDirectory(String[] arguments) {\n if (arguments.length == 0) {\n System.out.println("Missing directory argument.");\n return;\n }\n \n File directory = new File(arguments[0]);\n if (!directory.exists() || !directory.isDirectory()) {\n System.out.println("Directory not found: " + directory.getAbsolutePath());\n return;\n }\n \n if (directory.delete()) {\n System.out.println("Directory deleted: " + directory.getAbsolutePath());\n } else {\n System.out.println("Failed to delete directory: " + directory.getAbsolutePath());\n }\n }\n \n private static void removeFile(String[] arguments) {\n if (arguments.length == 0) {\n System.out.println("Missing file argument.");\n return;\n }\n \n File file = new File(arguments[0]);\n if (!file.exists() || file.isDirectory()) {\n System.out.println("File not found: " + file.getAbsolutePath());\n return;\n }\n \n if (file.delete()) {\n System.out.println("File deleted: " + file.getAbsolutePath());\n } else {\n System.out.println("Failed to delete file: " + file.getAbsolutePath());\n }\n }\n \n private static void createFile(String[] arguments) {\n if (arguments.length == 0) {\n System.out.println("Missing file argument.");\n return;\n }\n \n File file = new File(arguments[0]);\n if (file.exists()) {\n System.out.println("File already exists: " + file.getAbsolutePath());\n return;\n }\n \n try {\n if (file.createNewFile()) {\n System.out.println("File created: " + file.getAbsolutePath());\n } else {\n System.out.println("Failed to create file: " + file.getAbsolutePath());\n }\n } catch (IOException e) {\n System.out.println("Failed to create file: " + file.getAbsolutePath());\n e.printStackTrace();\n }\n }\n \n private static void changeFilePermissions(String[] arguments) {\n if (arguments.length == 0) {\n System.out.println("Missing file/directory argument.");\n return;\n }\n \n File file = new File(arguments[0]);\n if (!file.exists()) {\n System.out.println("File/directory not found: " + file.getAbsolutePath());\n return;\n }\n \n // Perform permission change here\n System.out.println("Changed permissions for: " + file.getAbsolutePath());\n }\n \n private static void printFileContent(String[] arguments) {\n if (arguments.length == 0) {\n System.out.println("Missing file argument.");\n return;\n }\n \n File file = new File(arguments[0]);\n if (!file.exists() || file.isDirectory()) {\n System.out.println("File not found: " + file.getAbsolutePath());\n return;\n }\n \n try {\n List<String> lines = Files.readAllLines(file.toPath());\n for (String line : lines) {\n System.out.println(line);\n }\n } catch (IOException e) {\n System.out.println("Failed to read file: " + file.getAbsolutePath());\n e.printStackTrace();\n }\n }\n \n private static void handleTarCommand(String[] arguments) {\n if (arguments.length < 3) {\n System.out.println("Invalid arguments for tar command.");\n return;\n }\n \n String option = arguments[0];\n String archiveFileName = arguments[1];\n String[] filesToArchive = Arrays.copyOfRange(arguments, 2, arguments.length);\n \n if (option.equalsIgnoreCase("-czvf")) {\n compressFiles(archiveFileName, filesToArchive);\n } else if (option.equalsIgnoreCase("-xzvf")) {\n extractFiles(archiveFileName, filesToArchive[0]);\n } else {\n System.out.println("Invalid option for tar command: " + option);\n }\n }\n \n private static void compressFiles(String archiveFileName, String[] filesToArchive) {\n // Perform file compression here\n System.out.println("Compressed files: " + Arrays.toString(filesToArchive) + " to " + archiveFileName);\n }\n \n private static void extractFiles(String archiveFileName, String targetDirectory) {\n // Perform file extraction here\n System.out.println("Extracted files from " + archiveFileName + " to " + targetDirectory);\n }\n \n private static void printCommandHistory() {\n for (int i = 0; i < commandHistory.size(); i++) {\n System.out.println((i + 1) + ". " + commandHistory.get(i));\n }\n }\n \n private static void printWorkingDirectory() {\n System.out.println("Current directory: " + System.getProperty("user.dir"));\n }\n}\n\n\n这是一个简单的示例程序,它可以处理您提到的常见命令,并支持命令历史记录。您可以根据需要进行修改和扩展。请注意,这只是一个基本示例,可能不包含所有错误处理和完整的功能。
原文地址: https://www.cveoy.top/t/topic/pBYe 著作权归作者所有。请勿转载和采集!