You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
7.6 KiB
Java
188 lines
7.6 KiB
Java
import com.matrixone.apps.domain.DomainObject;
|
|
import matrix.db.*;
|
|
import matrix.util.MatrixException;
|
|
|
|
import java.io.*;
|
|
import java.io.File;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Properties;
|
|
|
|
public class TMLInsertData_mxJPO {
|
|
|
|
public void InsertData(Context context, String[] args) throws Exception {
|
|
Map<String, Object> params = new HashMap<String, Object>();
|
|
params.put("SmallWalkInTestChamber", "小型步入式试验箱");
|
|
params.put("LargeWalkInTestChamber", "大型步入式试验箱(大舱)");
|
|
params.put("TemperatureAndHumidityChamber", "恒温恒湿箱");
|
|
params.put("HighAndLowTemperatureChamber", "高低温箱");
|
|
params.put("RackBox", "台架箱");
|
|
params.put("RapidTemperatureChange", "快温变");
|
|
params.put("TwoImpactBoxes", "两箱冲击箱");
|
|
params.put("ThreeImpactBoxes", "三箱冲击箱");
|
|
params.put("ThreeComprehensiveBoxes", "三综合箱");
|
|
params.put("DirectCoolingMachine", "直冷机");
|
|
params.put("Chiller", "冷水机");
|
|
params.put("Other", "其它");
|
|
|
|
// 设置正确的文件路径
|
|
// 在 Enovia/Matrix 环境中,建议使用绝对路径或相对项目路径
|
|
// String filePath = "/opt/MatrixOne/jpo/File/liangqi.properties"; // Linux 路径示例
|
|
// 或者使用相对路径
|
|
String filePath = "jpo/File/liangqi.properties";
|
|
|
|
System.out.println("=== 开始保存数据到文件 ===");
|
|
|
|
// 1. 保存数据到文件
|
|
saveToPropertiesFile(filePath, params);
|
|
|
|
System.out.println("\n=== 开始从文件读取数据并打印 ===");
|
|
|
|
// 2. 从文件读取数据并打印
|
|
Map<String, String> loadedParams = loadFromPropertiesFile(filePath);
|
|
|
|
// 3. 打印读取到的数据
|
|
printLoadedData(loadedParams);
|
|
|
|
System.out.println("\n=== 操作完成 ===");
|
|
}
|
|
|
|
// 保存为 Properties 文件
|
|
public static void saveToPropertiesFile(String filePath, Map<String, Object> params) throws Exception {
|
|
System.out.println("正在保存数据到: " + filePath);
|
|
|
|
// 确保目录存在
|
|
File file = new File(filePath);
|
|
File parentDir = file.getParentFile();
|
|
if (parentDir != null && !parentDir.exists()) {
|
|
boolean created = parentDir.mkdirs();
|
|
System.out.println("创建目录: " + parentDir.getPath() + " - " + (created ? "成功" : "失败"));
|
|
}
|
|
|
|
try (FileOutputStream fos = new FileOutputStream(filePath);
|
|
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8")) {
|
|
|
|
Properties props = new Properties();
|
|
|
|
// 添加所有参数
|
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
props.setProperty(entry.getKey(), entry.getValue().toString());
|
|
}
|
|
|
|
// 保存到文件,使用中文注释
|
|
props.store(osw, "设备类型映射表\n# 英文键名 = 中文名称");
|
|
|
|
System.out.println("✓ 数据已成功保存到文件: " + filePath);
|
|
System.out.println(" 保存了 " + params.size() + " 条记录");
|
|
|
|
// 打印保存的内容预览
|
|
System.out.println(" 保存的内容预览:");
|
|
int count = 0;
|
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
if (count++ < 3) { // 只显示前3条预览
|
|
System.out.println(" " + entry.getKey() + " = " + entry.getValue());
|
|
}
|
|
}
|
|
if (params.size() > 3) {
|
|
System.out.println(" ... 还有 " + (params.size() - 3) + " 条记录");
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
System.err.println("✗ 保存文件失败: " + e.getMessage());
|
|
e.printStackTrace();
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
// 从 Properties 文件读取
|
|
public static Map<String, String> loadFromPropertiesFile(String filePath) {
|
|
Map<String, String> loadedParams = new HashMap<>();
|
|
|
|
System.out.println("正在从文件读取: " + filePath);
|
|
|
|
// 检查文件是否存在
|
|
File file = new File(filePath);
|
|
if (!file.exists()) {
|
|
System.err.println("✗ 文件不存在: " + filePath);
|
|
return loadedParams;
|
|
}
|
|
|
|
if (!file.canRead()) {
|
|
System.err.println("✗ 文件不可读: " + filePath);
|
|
return loadedParams;
|
|
}
|
|
|
|
try (FileInputStream fis = new FileInputStream(filePath);
|
|
InputStreamReader isr = new InputStreamReader(fis, "UTF-8")) {
|
|
|
|
Properties props = new Properties();
|
|
props.load(isr);
|
|
|
|
// 转换为 Map
|
|
for (String key : props.stringPropertyNames()) {
|
|
loadedParams.put(key, props.getProperty(key));
|
|
}
|
|
|
|
System.out.println("✓ 从文件成功加载了 " + loadedParams.size() + " 条记录");
|
|
|
|
} catch (IOException e) {
|
|
System.err.println("✗ 读取文件失败: " + e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return loadedParams;
|
|
}
|
|
|
|
// 打印读取到的数据
|
|
public static void printLoadedData(Map<String, String> loadedParams) {
|
|
if (loadedParams == null || loadedParams.isEmpty()) {
|
|
System.out.println("警告: 没有读取到任何数据!");
|
|
return;
|
|
}
|
|
|
|
System.out.println("\n=== 打印所有读取的数据 ===");
|
|
System.out.println("序号 | 英文键名 | 中文名称");
|
|
System.out.println("----|---------------------------------------|--------------------------------");
|
|
|
|
int index = 1;
|
|
for (Map.Entry<String, String> entry : loadedParams.entrySet()) {
|
|
System.out.printf("%-4d| %-38s | %s%n",
|
|
index++,
|
|
entry.getKey(),
|
|
entry.getValue());
|
|
}
|
|
|
|
System.out.println("----|---------------------------------------|--------------------------------");
|
|
System.out.println("总计: " + loadedParams.size() + " 条记录");
|
|
|
|
// 额外的统计分析
|
|
System.out.println("\n=== 数据统计 ===");
|
|
System.out.println("包含 '箱' 的记录数: " +
|
|
loadedParams.values().stream().filter(v -> v.contains("箱")).count());
|
|
System.out.println("包含 '机' 的记录数: " +
|
|
loadedParams.values().stream().filter(v -> v.contains("机")).count());
|
|
}
|
|
|
|
// 添加一个专门的打印方法,用于在 Matrix 环境中显示
|
|
public static void printToMatrixConsole(Map<String, String> data) {
|
|
if (data == null || data.isEmpty()) {
|
|
System.out.println("No data to display.");
|
|
return;
|
|
}
|
|
|
|
System.out.println("=========================================");
|
|
System.out.println("DEVICE TYPE MAPPING TABLE");
|
|
System.out.println("=========================================");
|
|
System.out.println("ENGLISH KEY CHINESE NAME");
|
|
System.out.println("-----------------------------------------");
|
|
|
|
for (Map.Entry<String, String> entry : data.entrySet()) {
|
|
System.out.println(String.format("%-30s : %s",
|
|
entry.getKey(),
|
|
entry.getValue()));
|
|
}
|
|
|
|
System.out.println("=========================================");
|
|
System.out.println("Total records: " + data.size());
|
|
}
|
|
} |