【Java】多行文本转换格式导出

首先准备需要转换的文件

 可用于多种场景多行文本统一处理为统一格式,对多行文本数据逐一加解密,对多行文本筛选等等。对应的操作只需要修改文本处理逻辑部分

 代码:

// 多行文本转换

public class TestCreate {
    public static void main(String[] args) throws Exception {

        Map<Integer, String> rowDataMap = readTxtFile("预处理文本位置.txt");
        List<String> textList1 = new ArrayList<String>(10000);
        Set<String> textList = new HashSet<>(10000);

        Iterator<Integer> iterator = rowDataMap.keySet().iterator();
        while (iterator.hasNext()) {
            Integer key = iterator.next();
            String rowdata = rowDataMap.get(key);
            String retdata = "";
            //逻辑处理
                retdata = "orgCodes.add(\""+rowdata+"\");";

            // 对文件进行行合并
            textList.add(retdata);
        }

        // 将内容写入文件
        writeTxtFile("处理后文本位置.txt", textList);

    }

    /*

     * 根据文件路径读取文件
     *
     * @param filePath 文件路径
     * * @return Map<Integer, String>

    */

    private static Map<Integer, String> readTxtFile(String filePath) {

        Map<Integer, String> textMap = new HashMap<Integer, String>(10000);

        try (FileInputStream fileInputStream = new FileInputStream(filePath);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);) {

            String line = null;
            Integer i = 0;
            while ((line = bufferedReader.readLine()) != null) {
                i = i + 1;
                textMap.put(i, line);
            }

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

        }

        return textMap;
    }

    /*
     * 写入文件
     *
     * @param filePath 文件路径
     * @param textList 文件行内容
     * @throws IOException 异常信息
    */
    private static void writeTxtFile(String filePath, Set<String> textList) throws IOException {

        File file = new File(filePath);
        file.createNewFile();

        try (FileWriter fileWriter = new FileWriter(file);
                BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);) {
            for (String text : textList) {
                bufferedWriter.write(text);
                bufferedWriter.newLine();
            }

            bufferedWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

输出文件:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值