java 文件生成工具类

加载文件

       InputStream file = null;
        try {
            ClassPathResource classPathResource = new ClassPathResource("ab155/" + gameType + "/attr");
            file = classPathResource.getInputStream();
            return IOUtils.toString(file, StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

加载文件文件夹下第一个文件

        try {
            ClassPathResource dirResource = new ClassPathResource("ab157/" + gameType + "/");
            File dir = dirResource.getFile();
            File[] files = dir.listFiles();
            if (files == null || files.length == 0) {
                System.err.println("目录为空: " + dir.getAbsolutePath());
                return null;
            }
            File firstFile = files[0];
            return IOUtils.toString(
                    new FileInputStream(firstFile),
                    StandardCharsets.UTF_8
            );
        } catch (Exception e) {
            e.printStackTrace();
        }

遍历文件

public static void genNote(){
            String basePath = "D:\\code\\";
            String savePath = basePath + "tt.txt";
            Path path = Paths.get(basePath);
            StringBuilder stringBuilder = new StringBuilder();
            try (Stream paths = Files.walk(path)) {
                paths.filter(Files::isRegularFile)  // 只获取文件,排除目录
                        .forEach(filePath -> {
                            System.out.println("文件: " + filePath.toString());
                            try {
                                String s = FileUtils.readFileToString(new File(filePath.toString()), "utf-8");
                                JSONObject jsonObject = JSONObject.parseObject(s);
                                String gameName = jsonObject.getString("gameName");
                                stringBuilder.append("\n");
                                stringBuilder.append(gameName + " --------------");
                                stringBuilder.append("\n");
                                JSONArray templateArr = jsonObject.getJSONArray("templates");
                                for (int i = 0; i < templateArr.size(); i++) {
                                    JSONObject jsonObject1 = JSONObject.parseObject(templateArr.get(i).toString());
                                    String id = jsonObject1.getString("id");
                                    String field_name = jsonObject1.getString("field_name");
                                    String field_type = jsonObject1.getString("field_type");
                                    String is_required = jsonObject1.getString("is_required");

                                    stringBuilder.append("\t\t\t\t");
                                    stringBuilder.append( "// " + field_name  +"  " + id  +"   " + field_type );
                                    if(is_required.equals("true")){
                                        stringBuilder.append( "  is_required : " + is_required);
                                    }
                                    if("input".equals(field_type) || "number".equals(field_type)){
                                        stringBuilder.append("\n");
                                        stringBuilder.append("\t\t\t\t");
                                        stringBuilder.append( "util157.addDataValues(gameData,"+id+",sysRecovery,jsonObject.getString(\" \"));");
                                    } else if ("checkbox".equals(field_type)) {
                                        stringBuilder.append("\n");
                                        stringBuilder.append("\t\t\t\t");
                                        stringBuilder.append( "util157.addDataValues(gameData,"+id+",sysRecovery,null);");
                                    }
                                    stringBuilder.append("\n\n");
                                }
                                stringBuilder.append("\n");
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }

                        });
            } catch (IOException e) {
                e.printStackTrace();
            }

        try {
            FileUtils.writeStringToFile(new File(savePath), stringBuilder.toString(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

生成json文件

public void tt() {
        List gameInfos = gameInfoMapper.selectGameInfoList(new GameInfo());
        JSONArray allGames = new JSONArray();

        for (GameInfo gameInfo : gameInfos) {
            JSONObject finalJson = new JSONObject();
            finalJson.put("gameId", gameInfo.getId());
            finalJson.put("gameName", gameInfo.getName());

            // 查询模板
            GameAttrTemplate tpl1 = new GameAttrTemplate();
            tpl1.setFieldType("radio");
            tpl1.setGameId(gameInfo.getId());
            List list1 = gameAttrTemplateMapper.selectGameAttrTemplateList(tpl1);

            GameAttrTemplate tpl2 = new GameAttrTemplate();
            tpl2.setFieldType("boxradio");
            tpl2.setGameId(gameInfo.getId());
            List list2 = gameAttrTemplateMapper.selectGameAttrTemplateList(tpl2);

            List templateList = new ArrayList<>();
            if (list1 != null) templateList.addAll(list1);
            if (list2 != null) templateList.addAll(list2);

            if (templateList.isEmpty()) {
                System.out.println("empty templates for game " + gameInfo.getId());
                continue; // 不 return
            }

            JSONObject groupJson = new JSONObject();
            for (GameAttrTemplate tpl : templateList) {
                Long groupId = tpl.getAttributeId();
                String groupName = tpl.getFieldName();

                GameAttribute query = new GameAttribute();
                query.setParentId(groupId);
                List attrList = gameAttributeMapper.selectGameAttributeList(query);

                if (attrList == null || attrList.isEmpty()) continue;

                // 每个 group 是 key-value 对象
                JSONObject data = new JSONObject();
                for (GameAttribute attr : attrList) {
                    data.put(attr.getId().toString(), attr.getFieldName());
                }

                groupJson.put(groupName, data);
            }

            finalJson.put("data", groupJson);
            allGames.add(finalJson);
        }

        System.out.println(allGames.toJSONString());
        FileUtil.writeUtf8String(allGames.toJSONString(), "tt");

    }
暂无评论

发送评论 编辑评论


				
上一篇