QQ群请加: 6539042(powerbuilder11&SQL)  

一。文本校验,因为要工作在window,linux,所以用java写app

package com.cttmis;

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;

/**
因为客户导入的数据经常有一些错误,为了自动检查文件而开发
20070907
parm:文件所在路径 体积限制(M为单位)
cmd: java com.cttmis.checktxt 目录 体积限制(300)
异常一:行数据不完整。校验逗号分隔符号(,)的数量是否一致。(中间折行的也能校验出)
异常二:存在空行
我现在发现的错误是字符字段切断形成乱码而造成的。
*/

public class checktxt {
  public static void main(String[] args) {
    System.out.println("//");
    System.out.println("Txt文本校验工具(Java)v1.01/r/n异常一:行数据不完整。校验逗号分隔符号(,)的数量在每行是否一致。/r/n"+
                       "       (中间折行的,数据缺少的,都应该能校验出)/r/n异常二:存在空行(空白行,或跳行)/r/n实际上只要校验每行的分隔符数量即可。/r/nchengg0769 2007-09-06");
    System.out.println("//");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm");
    Date dates;
    dates= new Date();
    long timebegin=dates.getTime();
    System.out.println("日期:"+ sdf.format(dates));
    System.out.println();


    if (args.length <1) {
      System.out.println("请输入文件名或者文件夹名称!");
      return;
    }

    final  char SPLITERCOL = ',';  //列分隔符号
    long FileLengthMax = 300;  //300M限制

    if (args.length ==2){
      FileLengthMax = Long.parseLong(args[1]);
    }

    File[] filelist;

    try
    {
        File f = new File(args[0]);
        if (f.isDirectory())
        {
          filelist = f.listFiles(new txtFilenameFilter("txt"));
          System.out.println("文件清单: -----------------------begin----------------------------");
          printFileNameArray(filelist); //Out print file name
          System.out.println("文件清单: -----------------------end------------------------------");
          System.out.println();

          FileReader fr;
          BufferedReader br;
          String LineString; //单行数据
          String LastLineString; //上行数据,便于比较

          int TotalRows =0;
          int QtyCommaPerLine = 0;  //标准分隔符数量
          int QtyComma = 0;  //标准分隔符数量
          int ErrCount = 0;  //错误计数器
          int SleepIdle=0;

          for (int i = 0; i < filelist.length; i++) {
            if (filelist[i].length() > 1000000*FileLengthMax){
              System.out.println("体积超过限制的:" + FileLengthMax + "M,程序不检查,进入下一个!");
              continue;
            }
          Thread.sleep(1000);
          fr = new FileReader(filelist[i]);
          br = new BufferedReader(fr);

          SleepIdle = 0;
          TotalRows =0;
          ErrCount = 0;
          LineString = "";
          LastLineString = "";
          System.out.println(filelist[i].getName() + " -----------------begin--------------");
          while ( (LineString = br.readLine()) != null) {
               switch (++TotalRows)
                  {
                  case 1:
                      //取得参考值
                      QtyCommaPerLine = QtyCharInString(LineString,SPLITERCOL);
                      LastLineString = LineString;
                      break;
                  default:
                      QtyComma = QtyCharInString(LineString,SPLITERCOL);
                      //异常一:行数据不完整。校验逗号分隔符号(,)的数量是否一致。
                      //(中间折行的也能校验出)
                      if (QtyComma != QtyCommaPerLine)
                       {
                         ErrCount++;
                         System.out.println(filelist[i].getName() + " 行: "+ TotalRows++ + " 分隔符数量异常!标准数量:" +
                                            QtyCommaPerLine + " 实际数量: " + QtyComma);
                         System.out.println("  上一行>>"+ LastLineString);
                         System.out.println("  错误行>>"+ LineString);
                       }
                      //异常二:存在空行
                      else if (LineString == "")
                       {
                         ErrCount++;
                         System.out.println("  上一行>>"+ LastLineString);
                         System.out.println(filelist[i].getName() + " 行: "+ TotalRows++ + " 该行为空白行!");
                       }
                       LastLineString = LineString;
                       if (SleepIdle++ ==100000)
                       {
                       Thread.sleep(100);
                       SleepIdle = 0;
                       }
                  }
            }
            if (ErrCount == 0)
            {
              System.out.println(filelist[i].getName() + " 通过校验,OK");
            }
            else
            {
              System.out.println(filelist[i].getName() + " 存在异常:共 " + ErrCount + " 行");
            }
            System.out.println(filelist[i].getName() + "总行数: " + TotalRows+
                              "  错误行数: " + ErrCount + " -----------------end----------------");
            System.out.println();
            br.close();
          }
        }
        long timeend=new Date().getTime();
        System.out.println("工作耗时:"+(timeend - timebegin)/1000 + "秒");
        System.gc();

    }catch(Exception e)
    {
        e.printStackTrace();
    }
  }
  static void printFileNameArray(File[] filelist)
  {
      for(int i=0;i<filelist.length;i++)
      {
          System.out.println(filelist[i].getName());
      }
  }

  //查找某个字符在字符穿中出现的个数
  static int QtyCharInString(String s,char c)
  {
    int   count   =   0;
    for(int i=0;i<s.length();i++)
    {
      if(s.charAt(i)==c)
        count++;
    }
    return   count;
  }
}

//filter interface class
class txtFilenameFilter implements FilenameFilter
{
   String ext;

   txtFilenameFilter(String ext){
      this.ext= "." +ext;
    }

    public boolean accept(File dir,String name)
    {
      return name.endsWith(ext);
    }

}

二。bat批处理(window下)

1. 不输出文本,只作屏幕显示:

@@echo off

set path = "%path%;C:/Program Files/Java/j2re1.4.2_04/bin"

java -classpath "%classpath%;." com.cttmis.checktxt E:/FTPFILE
pause

2. 作检查并输出到err.log

@@echo off

set path = "%path%;C:/Program Files/Java/j2re1.4.2_04/bin"

java -classpath "%classpath%;." com.cttmis.checktxt E:/FTPFILE >>err.log
pause

//Note: "%classpath%;." 后面的点代表当前路径,这样不用关心class文件的路径名。

//这是一个习作,是学习java的第一个程序。也许有不足的地方。

Logo

更多推荐