用Java实现类似Ubuntu终端的程序:命令行解释器示例
{"title":"用Java实现类似Ubuntu终端的程序:命令行解释器示例","description":"本文提供了一个用Java编写的类似Ubuntu终端的程序示例,支持常用命令,例如ls、cd、mkdir、rmdir、rm、touch、chmod、cat、tar、history、pwd等,并允许向上翻动命令历史。","keywords":"Java, 终端, 命令行, 命令行解释器, Ubuntu, ls, cd, mkdir, rmdir, rm, touch, chmod, cat, tar, history, pwd, 命令历史","content":"要实现一个类似Ubuntu终端的程序,你可以使用Java编写一个控制台应用程序。以下是一个示例代码,满足你提供的要求:\n\njava\nimport java.io.BufferedReader;\nimport java.io.File;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.nio.file.Files;\nimport java.nio.file.Paths;\nimport java.nio.file.StandardCopyOption;\nimport java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.List;\n\npublic class Terminal {\n\n private List<String> commandHistory;\n private String currentDirectory;\n\n public Terminal() {\n commandHistory = new ArrayList<>();\n currentDirectory = System.getProperty("user.dir");\n }\n\n public void start() throws IOException {\n BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));\n String input;\n\n while (true) {\n System.out.print(currentDirectory + "> ");\n input = reader.readLine();\n\n if (input.equals("exit")) {\n break;\n }\n\n executeCommand(input);\n commandHistory.add(input);\n }\n }\n\n private void executeCommand(String input) throws IOException {\n String[] tokens = input.split("\\s+");\n String command = tokens[0];\n\n switch (command) {\n case "ls":\n executeLs(tokens);\n break;\n case "cd":\n executeCd(tokens);\n break;\n case "mkdir":\n executeMkdir(tokens);\n break;\n case "rmdir":\n executeRmdir(tokens);\n break;\n case "rm":\n executeRm(tokens);\n break;\n case "touch":\n executeTouch(tokens);\n break;\n case "chmod":\n executeChmod(tokens);\n break;\n case "cat":\n executeCat(tokens);\n break;\n case "tar":\n executeTar(tokens);\n break;\n case "history":\n executeHistory();\n break;\n case "pwd":\n executePwd();\n break;\n default:\n System.out.println("Unknown command: " + command);\n }\n }\n\n private void executeLs(String[] tokens) throws IOException {\n List<String> files;\n\n if (tokens.length > 1 && tokens[1].equals("-l")) {\n files = Arrays.asList(new File(currentDirectory).list());\n } else if (tokens.length > 1 && tokens[1].equals("-ld")) {\n files = Arrays.asList(new File(currentDirectory).list());\n System.out.println(currentDirectory);\n } else if (tokens.length > 1) {\n files = Arrays.asList(new File(tokens[1]).list());\n } else {\n files = Arrays.asList(new File(currentDirectory).list());\n }\n\n for (String file : files) {\n System.out.println(file);\n }\n }\n\n private void executeCd(String[] tokens) {\n if (tokens.length > 1) {\n String newDirectory = tokens[1];\n\n if (newDirectory.equals("..")) {\n currentDirectory = new File(currentDirectory).getParent();\n } else {\n currentDirectory = Paths.get(currentDirectory, newDirectory).toString();\n }\n\n System.setProperty("user.dir", currentDirectory);\n }\n }\n\n private void executeMkdir(String[] tokens) throws IOException {\n if (tokens.length > 1) {\n String newDirectory = tokens[1];\n Files.createDirectory(Paths.get(currentDirectory, newDirectory));\n }\n }\n\n private void executeRmdir(String[] tokens) throws IOException {\n if (tokens.length > 1) {\n String directoryToDelete = tokens[1];\n Files.delete(Paths.get(currentDirectory, directoryToDelete));\n }\n }\n\n private void executeRm(String[] tokens) throws IOException {\n if (tokens.length > 1) {\n String fileToDelete = tokens[1];\n\n if (tokens.length > 2 && tokens[2].equals("-rf")) {\n Files.walk(Paths.get(currentDirectory, fileToDelete))\n .sorted((path1, path2) -> -path1.toString().length())\n .map(Path::toFile)\n .forEach(File::delete);\n } else {\n Files.delete(Paths.get(currentDirectory, fileToDelete));\n }\n }\n }\n\n private void executeTouch(String[] tokens) throws IOException {\n if (tokens.length > 1) {\n String newFile = tokens[1];\n Files.createFile(Paths.get(currentDirectory, newFile));\n }\n }\n\n private void executeChmod(String[] tokens) throws IOException {\n if (tokens.length > 1) {\n String fileOrDir = tokens[1];\n String permission = tokens[2];\n\n File target = new File(currentDirectory, fileOrDir);\n target.setReadable(permission.charAt(0) == '1');\n target.setWritable(permission.charAt(1) == '1');\n target.setExecutable(permission.charAt(2) == '1');\n }\n }\n\n private void executeCat(String[] tokens) throws IOException {\n if (tokens.length > 1) {\n String fileToRead = tokens[1];\n Files.lines(Paths.get(currentDirectory, fileToRead)).forEach(System.out::println);\n }\n }\n\n private void executeTar(String[] tokens) throws IOException {\n if (tokens.length > 1) {\n String option = tokens[1];\n String archiveName = tokens[2];\n\n if (option.equals("-czvf")) {\n String[] filesToArchive = Arrays.copyOfRange(tokens, 3, tokens.length);\n ArchiveUtils.compress(filesToArchive, archiveName);\n } else if (option.equals("-xzvf")) {\n String destinationPath = tokens[3];\n ArchiveUtils.extract(archiveName, destinationPath);\n }\n }\n }\n\n private void executeHistory() {\n for (int i = 0; i < commandHistory.size(); i++) {\n System.out.println(i + " " + commandHistory.get(i));\n }\n }\n\n private void executePwd() {\n System.out.println(currentDirectory);\n }\n\n public static void main(String[] args) throws Exception {\n Terminal terminal = new Terminal();\n terminal.start();\n }\n}\n\n\n此示例是一个简单的命令行解释器,可以处理你提供的常用命令。它使用了Java的标准库和一些文件操作来模拟命令行功能。请注意,此示例可能不包括错误处理和完整的功能,你可以根据需要进行扩展和修改。
原文地址: https://www.cveoy.top/t/topic/pBXG 著作权归作者所有。请勿转载和采集!