java实现英文转摩尔斯电码传输(一)
摩尔斯电码简介:
摩尔斯电码(Morse code)也被称作摩斯密码,是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母、数字和标点符号。它发明于1837年,是一种早期的数字化通信形式。不同于现代化的数字通讯,摩尔斯电码只使用零和一两种状态的二进制代码,它的代码包括五种:短促的点信号“・”,保持一定时间的长信号“—”,表示点和划之间的停顿、每个词之间中等的停顿,以及句子之间长的停顿。
文章简介:
这个系列的目的为了讲解,java连接数据库,java文件读取,java如何网络传输这些功能,后面会把这个文章不断的完善。
数据库建表语句
这是数据库建表成功之后的样子。
/* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Type : MySQL Source Server Version : 80028 Source Host : localhost:3306 Source Schema : projectdatabase Target Server Type : MySQL Target Server Version : 80028 File Encoding : 65001 Date: 30/01/2023 14:54:25 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for morsecodemappingtable -- ---------------------------- DROP TABLE IF EXISTS `morsecodemappingtable`; CREATE TABLE `morsecodemappingtable` ( `id` int NOT NULL AUTO_INCREMENT, `letter` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 62 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Records of morsecodemappingtable -- ---------------------------- INSERT INTO `morsecodemappingtable` VALUES (1, 'A', '01'); INSERT INTO `morsecodemappingtable` VALUES (2, 'B', '1000'); INSERT INTO `morsecodemappingtable` VALUES (3, 'C', '1010'); INSERT INTO `morsecodemappingtable` VALUES (4, 'D', '100'); INSERT INTO `morsecodemappingtable` VALUES (5, 'E', '0'); INSERT INTO `morsecodemappingtable` VALUES (6, 'F', '0010'); INSERT INTO `morsecodemappingtable` VALUES (7, 'G', '110'); INSERT INTO `morsecodemappingtable` VALUES (8, 'H', '0000'); INSERT INTO `morsecodemappingtable` VALUES (9, 'I', '00'); INSERT INTO `morsecodemappingtable` VALUES (10, 'J', '0111'); INSERT INTO `morsecodemappingtable` VALUES (11, 'K', '101'); INSERT INTO `morsecodemappingtable` VALUES (12, 'L', '0100'); INSERT INTO `morsecodemappingtable` VALUES (13, 'M', '11'); INSERT INTO `morsecodemappingtable` VALUES (14, 'N', '10'); INSERT INTO `morsecodemappingtable` VALUES (15, 'O', '111'); INSERT INTO `morsecodemappingtable` VALUES (16, 'P', '0110'); INSERT INTO `morsecodemappingtable` VALUES (17, 'Q', '1101'); INSERT INTO `morsecodemappingtable` VALUES (18, 'R', '010'); INSERT INTO `morsecodemappingtable` VALUES (19, 'S', '000'); INSERT INTO `morsecodemappingtable` VALUES (20, 'T', '1'); INSERT INTO `morsecodemappingtable` VALUES (21, 'U', '001'); INSERT INTO `morsecodemappingtable` VALUES (22, 'V', '0001'); INSERT INTO `morsecodemappingtable` VALUES (23, 'W', '011'); INSERT INTO `morsecodemappingtable` VALUES (24, 'X', '1001'); INSERT INTO `morsecodemappingtable` VALUES (25, 'Y', '1011'); INSERT INTO `morsecodemappingtable` VALUES (26, 'Z', '1100'); INSERT INTO `morsecodemappingtable` VALUES (27, '0', '11111'); INSERT INTO `morsecodemappingtable` VALUES (28, '1', '01111'); INSERT INTO `morsecodemappingtable` VALUES (29, '2', '00111'); INSERT INTO `morsecodemappingtable` VALUES (30, '3', '00011'); INSERT INTO `morsecodemappingtable` VALUES (31, '4', '00001'); INSERT INTO `morsecodemappingtable` VALUES (32, '5', '00000'); INSERT INTO `morsecodemappingtable` VALUES (33, '6', '10000'); INSERT INTO `morsecodemappingtable` VALUES (34, '7', '11000'); INSERT INTO `morsecodemappingtable` VALUES (35, '8', '11100'); INSERT INTO `morsecodemappingtable` VALUES (36, '9', '11110'); INSERT INTO `morsecodemappingtable` VALUES (37, '.', '010101'); INSERT INTO `morsecodemappingtable` VALUES (38, ':', '111000'); INSERT INTO `morsecodemappingtable` VALUES (39, ',', '110011'); INSERT INTO `morsecodemappingtable` VALUES (40, ';', '101010'); INSERT INTO `morsecodemappingtable` VALUES (41, '?', '001100'); INSERT INTO `morsecodemappingtable` VALUES (42, '=', '10001'); INSERT INTO `morsecodemappingtable` VALUES (43, '\'', '011110'); INSERT INTO `morsecodemappingtable` VALUES (44, '/', '10010'); INSERT INTO `morsecodemappingtable` VALUES (45, '!', '101011'); INSERT INTO `morsecodemappingtable` VALUES (46, '-', '100001'); INSERT INTO `morsecodemappingtable` VALUES (47, '_', '001101'); INSERT INTO `morsecodemappingtable` VALUES (48, '\"', '010010'); INSERT INTO `morsecodemappingtable` VALUES (49, '(', '10110'); INSERT INTO `morsecodemappingtable` VALUES (50, ')', '101101'); INSERT INTO `morsecodemappingtable` VALUES (51, '$', '0001001'); INSERT INTO `morsecodemappingtable` VALUES (52, '&', '01000'); INSERT INTO `morsecodemappingtable` VALUES (53, '@', '011010'); INSERT INTO `morsecodemappingtable` VALUES (54, '+', '01010'); INSERT INTO `morsecodemappingtable` VALUES (55, 'AA', '0101'); INSERT INTO `morsecodemappingtable` VALUES (56, 'AR', '01010'); INSERT INTO `morsecodemappingtable` VALUES (57, 'AS', '01000'); INSERT INTO `morsecodemappingtable` VALUES (58, 'BK', '1000101'); INSERT INTO `morsecodemappingtable` VALUES (59, 'BT', '10001'); INSERT INTO `morsecodemappingtable` VALUES (60, 'CT', '10101'); INSERT INTO `morsecodemappingtable` VALUES (61, 'SK', '000101'); INSERT INTO `morsecodemappingtable` VALUES (62, 'SOS', '000111000'); SET FOREIGN_KEY_CHECKS = 1;
项目结构
pom.xml
``xml
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
<groupId>org.example</groupId> <artifactId>telegraph</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version> </dependency> <!-- https://mvnrepository.com/artifact/org/jaudiotagger --> <dependency> <groupId>org</groupId> <artifactId>jaudiotagger</artifactId> <version>2.0.3</version> </dependency> </dependencies>
```
链接数据库的方法
我们把链接数据库,封装成一个方法,然后反悔这个数据库查询的结果,就是查询这个数据库,然后把这数据库中的内容查询出来,然后封装成一个列表。
public static ArrayList<HashMap<String, String> > DatabaseConnection() throws Exception{ // 这个方法负责连接数据库 并把获取的值做成映射表返回 HashMap<String, String> hashMap1 = new HashMap<>(); HashMap<String, String> hashMap2 = new HashMap<>(); Class.forName("com.mysql.cj.jdbc.Driver"); //2. 获取连接 // String url = "jdbc:mysql://127.0.0.1:3306/books"; // 如果是mysql5这样写 String url = "jdbc:mysql://localhost:3306/projectdatabase?serverTimezone=GMT%2B8&useOldAliasMetadataBehavior=true"; // 如果是mysql8这样写 String username = "root"; String password = "123456"; Connection conn = DriverManager.getConnection(url, username, password); //3. 定义sql String sql = "SELECT * FROM morsecodemappingtable"; //4. 获取执行sql的对象 Statement Statement stmt = conn.createStatement(); //5. 执行sql ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ String letter = rs.getString(2); String code = rs.getString(3); hashMap1.put(letter, code); hashMap2.put(code, letter); } ArrayList<HashMap<String, String> >arrayList = new ArrayList<>(); arrayList.add(hashMap1); arrayList.add(hashMap2); return arrayList; }
Code类
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Code { private String letter; private String code; }
把原文文本转化成摩尔斯电码
在Scanner文本中写入你需要转化的文本。
然后通过下面这个方法,这是一个加密函数,讲获取的文本转化成对应的摩尔斯电码。
// 加密函数 public static String toCode(String text, HashMap<String, String> hashMap){ String[] strs = text.toUpperCase().split(" "); String text2 = ""; for (String x : strs){ // System.out.println(x); for (int i = 0; i < x.length(); ++ i){ text2 += hashMap.get(x.charAt(i)+"") + " "; } } text2.trim(); return text2; }
把摩尔斯电码转化成对应文本
这个方法的目的是解密,把获取的摩尔斯电码进行解密,转换成对应的文本,就是我们需要注意的是,这个摩尔斯电码需要的是用空格,将每个字母的对应的电码隔开,这样更加方便于解密。
// 解密函数 public static String toText(String code, HashMap<String, String> hashMap){ String[] strs = code.toLowerCase().split(" "); String text2 = ""; for (String x : strs){ // System.out.println(x); text2 += hashMap.get(x+"") + " "; } // 单词都是小写的话好看一点 text2 = text2.trim().toLowerCase(); return text2; }
完整代码
import org.jaudiotagger.audio.AudioFileIO; import org.jaudiotagger.audio.mp3.MP3AudioHeader; import org.jaudiotagger.audio.mp3.MP3File; import java.io.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; public class Main { public static ArrayList<HashMap<String, String> > DatabaseConnection() throws Exception{ // 这个方法负责连接数据库 并把获取的值做成映射表返回 HashMap<String, String> hashMap1 = new HashMap<>(); HashMap<String, String> hashMap2 = new HashMap<>(); Class.forName("com.mysql.cj.jdbc.Driver"); //2. 获取连接 // String url = "jdbc:mysql://127.0.0.1:3306/books"; // 如果是mysql5这样写 String url = "jdbc:mysql://localhost:3306/projectdatabase?serverTimezone=GMT%2B8&useOldAliasMetadataBehavior=true"; // 如果是mysql8这样写 String username = "root"; String password = "123456"; Connection conn = DriverManager.getConnection(url, username, password); //3. 定义sql String sql = "SELECT * FROM morsecodemappingtable"; //4. 获取执行sql的对象 Statement Statement stmt = conn.createStatement(); //5. 执行sql ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ String letter = rs.getString(2); String code = rs.getString(3); hashMap1.put(letter, code); hashMap2.put(code, letter); } ArrayList<HashMap<String, String> >arrayList = new ArrayList<>(); arrayList.add(hashMap1); arrayList.add(hashMap2); return arrayList; } // 加密函数 public static String toCode(String text, HashMap<String, String> hashMap){ String[] strs = text.toUpperCase().split(" "); String text2 = ""; for (String x : strs){ // System.out.println(x); for (int i = 0; i < x.length(); ++ i){ text2 += hashMap.get(x.charAt(i)+"") + " "; } } text2.trim(); return text2; } // 解密函数 public static String toText(String code, HashMap<String, String> hashMap){ String[] strs = code.toLowerCase().split(" "); String text2 = ""; for (String x : strs){ // System.out.println(x); text2 += hashMap.get(x+"") + " "; } // 单词都是小写的话好看一点 text2 = text2.trim().toLowerCase(); return text2; } public static void main(String[] args) throws Exception{ // 初始化连接数据库 ArrayList<HashMap<String, String> > hashMap = DatabaseConnection(); // letter->code 加密哈希表 HashMap<String, String> hashMap1 = hashMap.get(0); // code->letter 解密哈希表 HashMap<String, String> hashMap2 = hashMap.get(1); // 获取的文本 String text = ""; // System.out.println(hashMap1); // System.out.println(hashMap2); // 将文本转化为摩尔斯电码 BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/texts/Scanner.txt")); BufferedWriter writer = new BufferedWriter(new FileWriter("src/main/resources/texts/Code.txt")); String line; while((line = reader.readLine()) != null){ // 去除首尾多余的空格 line = line.trim(); // 加空格是为了后面好分割数据 text = text + line + " "; // System.out.println(line); } text = text.trim(); // System.out.println(text); // 获取加密之后的文本 String code_text = toCode(text, hashMap1); writer.write(code_text); writer.flush(); // 一定要刷新后面 writer.close(); System.out.println(code_text); // 讲解密的内容存储到这个文本里面 writer = new BufferedWriter(new FileWriter("src/main/resources/texts/decrypt.txt")); String letter_text = toText(code_text, hashMap2); System.out.println(letter_text); writer.write(letter_text); writer.flush(); // 一定要刷新后面 writer.close(); } }
运行结果:
每个文本的情况
存储信息的原始文章的文本
加密之后的摩尔斯电码的文本
摩尔斯电码解密之后的文本。