算法小练

1.统计某一目录下每个文件中出现的字母个数、数字个数、空格个数及行数

import java.io.*;
import java.util.*;
import java.lang.*;
/**
*   统计某一目录下每个文件中出现的字母个数、数字个数、空格个数及行数
*/
public class TextStatisticsMain{
    public static void main(String args[]){        
        TextStatisticsMain tm = new TextStatisticsMain(args[0]);
        tm.print();
    }
    private List textStatistics = new ArrayList();
    private String rootPath;
    public TextStatisticsMain(String file){
        this.rootPath = file;
    }
    
    public void print(){
         statistics(new File(this.rootPath));
          System.out.println("==== Statistics ====");
          System.out.println("                     ");
        for(TextStatistics t:textStatistics){           
            System.out.println("File: "+t.getFileName());
            System.out.println("letCount: "+ t.getLetCount());
            System.out.println("numCount: "+ t.getNumCount());
            System.out.println("spaceCount: "+t.getSpaceCount());
            System.out.println("lines: "+t.getLines() );             
            System.out.println("----------------------------");
        }
    }
    private  void statistics(File file){             
        if(file.isDirectory()){
        //是目录 
            for(File f:file.listFiles()){
                statistics(f);
            }            
        }else{
        //是文件
          try{          
            FileReader fread = new FileReader(file);
            BufferedReader lnf = new BufferedReader(fread);
            String b;
            String fileName = file.getPath();
            int letCount=0, numCount=0,spaceCount=0, lines=0;            
            while ((b = lnf.readLine()) != null) {  
                lines++;               
                for(char c:b.toCharArray()){                
                  //对比ASCII码
                  if((int)c>=48 && (int)c<=57){
                  //是数字                   
                   numCount++;   
                  }else if((int)c==32){
                  //空格
                    spaceCount++;
                  }else if((int)c>=65 && (int)c<=90){
                  //大写字母
                    letCount++;
                  }else if((int)c>=97 && (int)c<=122){
                  //小写字母
                    letCount++;
                  }
                }
            }
                textStatistics.add(new TextStatistics(fileName,letCount,numCount,spaceCount,lines));
            }catch(Exception e){
                System.out.println(e);
            }
           
        }
    } 
}
public class TextStatistics{
    private String fileName;
    private int letCount;
    private int numCount;
    private int spaceCount;
    private int lines;
    public TextStatistics(String fileName,int letCount,int numCount,int spaceCount,int lines ){
        this.fileName=fileName;
        this.letCount=letCount;
        this.numCount=numCount;
        this.spaceCount=spaceCount;
        this.lines=lines;
    }
    public String getFileName(){
        return this.fileName;
    }
    public int getLetCount(){
        return this.letCount;
    }
    public int getNumCount(){
        return this.numCount;
    }
    public int getSpaceCount(){
        return this.spaceCount;
    }
    public int getLines(){
        return this.lines;
    }

}

2.现在一个表,有姓名、性别、年龄,字段分别为varchar(20),varchar(20),int,
现有表中已经有上百万条记录了,在姓名上建索引,现在有一个文本文件,已经格式化为姓名、性别、年龄,写一个函数,
怎么样一次性的将该文件中的数据以最快的速度导入到数据库中。
如有函数,实现该函数:
save(Connection c,File f)
{
//直接导入文件就行了,大概思路就是这样
Statement s = c.createStatement();
s.execute("LOAD DATA INFILE '"+f.getPath()+"' INTO TABLE tablename fields terminated by ',' lines terminated by '\n'");
}

This entry was posted in Program. Bookmark the permalink.