• JAVA 0 的突破


    使用IntelliJ Idea 开发的一个Java 处理数据文件折行的问题,整体来说功能比较简单的一个java脚本的开发,跨平台的优势可以处理windows和lunix平台的文件折行

    package com.company;
    import com.sun.javafx.image.BytePixelSetter;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.io.BufferedWriter;
    import java.io.Reader;
    import java.io.FileWriter;
    public class Main {
    
    
        public static boolean isNumeric(String str){
            for (int i = 0; i < str.length(); i++){
              //  System.out.println(str.charAt(i));
                if (!Character.isDigit(str.charAt(i))){
                    return false;
                }
            }
            return true;
        }
    
        public static void hrwtFilePorcess(String filePath){
            try {
                String encoding="UTF8";
                File file=new File(filePath);
                if(file.isFile() && file.exists()){ //判断文件是否存在
                    InputStreamReader read = new InputStreamReader(
                    new FileInputStream(file),encoding);//考虑到编码格式
                    BufferedReader bufferedReader = new BufferedReader(read);
    
    
    
                    String preLine=null;
                    String lineTxt = null;
                    String nextLineTxt=null;
                    StringBuffer sb=new StringBuffer();
    
    
                    boolean isTR=false;
                    int a =0;
                    while((lineTxt = bufferedReader.readLine()) != null ){
    
    
                        int index =lineTxt.indexOf("|");
                        String id =lineTxt.substring(0,index);
                        if(isNumeric(id)==false && preLine != null){
                            preLine+=lineTxt;
                            isTR=true;
                        }else {
                            isTR=false;
                            if(a!=0)
                            sb.append("
    ");
                            preLine=lineTxt;
                        }
    
                        sb.append(lineTxt);
                        a++;
    
    
                    }
    
                    read.close();
                    System.out.println("开始替换");
                    BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
                    out.write(sb.toString());
                    out.flush();
                    out.close();
                    System.out.println("替换完成:"+filePath);
                }else{
                    System.out.println("找不到指定的文件");
                }
            } catch (Exception e) {
                System.out.println("读取文件内容出错");
                e.printStackTrace();
            }
    
        }
    
        public static void sapFileProcess(String filePath){
            try {
                String encoding="UTF8";
                File file=new File(filePath);
                if(file.isFile() && file.exists()){ //判断文件是否存在
                    InputStreamReader read = new InputStreamReader(
                            new FileInputStream(file),encoding);//考虑到编码格式
                    BufferedReader bufferedReader = new BufferedReader(read);
    
                    String lineTxt = null;
    
                    StringBuffer sb=new StringBuffer();
                    boolean isTR=false;
                    int a =0;
                    while((lineTxt = bufferedReader.readLine()) != null ) {
                        //5行之后如果不是现开头就是折行
                        if (a >=6) {
                            int index = lineTxt.indexOf("|");
                            int index2 =lineTxt.indexOf("-------");
    
                            if (index > 1 && index2!=0 ) {
                                sb.append(lineTxt);
    
                            } else {
                                sb.append("
    ");
                                sb.append(lineTxt);
    
    
                            }
                        }
                       else
                       {
    
                           if(a>0){
                               sb.append("
    ");
                           }
                           sb.append(lineTxt);
    
                        }
                        a++;
                    }
                    BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
                    out.write(sb.toString());
                    out.flush();
                    out.close();
                    System.out.println("替换完成:"+filePath);
                }else{
                    System.out.println("找不到指定的文件");
                }
            } catch (Exception e) {
                System.out.println("读取文件内容出错");
                e.printStackTrace();
            }
    
        }
    
    
        public static void main(String[] args) {
    	// write your code here
    
            if(args.length>0){
    
                for (int i=0 ; i<args.length; i++   )
                {
    
                    System.out.println((i));
                    try
                    {
    
                    System.out.println(args[i]);
                    String configPath=args[i];
                        System.out.println("configPath:"+configPath);
    
                    File file=new File(configPath);
                    String encoding="UTF8";
                    if(file.isFile() && file.exists()) { //判断文件是否存在
                        InputStreamReader read = new InputStreamReader(
                                new FileInputStream(file),encoding);//考虑到编码格式
                        BufferedReader bufferedReader = new BufferedReader(read);
                        String lineTxt = null;
                        while((lineTxt = bufferedReader.readLine()) != null ){
    
    
                            String[] str =lineTxt.split("\|");
                            String filePath=str[1];
                            String type=str[0];
                            String sap="sap";
    
                            System.out.println("filePath:"+filePath);
                            System.out.println("type:"+type + ": "+sap+ (type.equals(sap)));
    
                            if(type.equals(sap)){
                                System.out.println("sapFileProcess:");
                                sapFileProcess(filePath);
                            }else   {
                                System.out.println("hrwtFilePorcess:");
    
                                hrwtFilePorcess(filePath);
                            }
    
                        }
    
                    }else   {
    
                        System.out.println("configPath:"+configPath +"; 配置文件不存在 ");
    
                    }
                    }
                    catch (Exception e) {
                        System.out.println("读取文件内容出错");
                        e.printStackTrace();
                    }
    
                }
            }else
            {
    
                System.out.println("请指定配置文件");
            }
    
    
            //test
            String filePath = "C:\Users\miachen\Desktop\data\hrwt\20170630-193314-CHN_PERSON.TXT";
            String sapFile = "C:\Users\miachen\Desktop\data\sap\KSB1_201703_2943.txt";
    
           // sapFileProcess(sapFile);
    
        }
    }
    

      

  • 相关阅读:
    Flask路由+视图补充
    Flask登录认证
    Flask
    初识Flask
    redis 注意事项
    Linux安装python和更新pip
    Django 导入配置文件
    redis 5种类型
    redis 支持事务
    数组乱序与数组拆解
  • 原文地址:https://www.cnblogs.com/hellohongfu/p/7283457.html
Copyright © 2020-2023  润新知