package com.qunar.fresh;
import java.io.*;
import java.util.Map;
import java.util.TreeMap;
/**
* Classname First.java
*
* Version information 1.0
*
* Created by deliang.gao on 2015/11/2.
*
*/
public class First {
static String line = null ;
static Map map = new TreeMap();
static String s = null ;
static Integer numInt = null ;
static int num = 0 ;
static int temp = 0 ;
static int lineNum = 0 ;
static int numCount = 0 ;
static int letterCount = 0 ;
static int chineseCount = 0 ;
public static void main (String args[]) {
String path = First.class.getResource("/" ).getPath() +"files" ;
ReadDirectory(new File(path));
printAll();
}
public static void ReadDirectory (File dir){
if (dir.isDirectory()){
File[] subFile=dir.listFiles();
for (int i=0 ;i<subFile.length;i++)
if (subFile[i].isDirectory()){
ReadDirectory(subFile[i]);
}else {
ReadSubFile(subFile[i]);
}
}
}
public static void ReadSubFile (File file){
BufferedReader reader = null ;
try {
reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null ) {
lineNum++;
for (int i = 0 ; i < line.length(); i++) {
s = String.valueOf(line.charAt(i));
temp = line.charAt(i);
if (temp>='0' &&temp<='9' ){
numCount++;
}else if ((temp>='a' &&temp<='z' )||(temp>='a' &&temp<='z' )){
letterCount++;
}else {
chineseCount++;
}
numInt = (Integer) map.get(s);
if (numInt == null ) {
map.put(s, Integer.valueOf(1 ));
} else {
num = numInt.intValue();
map.put(s, Integer.valueOf(num + 1 ));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void printAll (){
BufferedWriter writer = null ;
try {
writer=new BufferedWriter(new FileWriter(First.class.getResource("/" ).getPath()+"config" ));
writer.write("行数为" +lineNum+"\r\n" );
writer.write("数字个数:" +numCount+"\r\n" );
writer.write("字母个数:" +letterCount+"\r\n" );
writer.write("汉字个数:" +chineseCount+"\r\n" );
String[] keys = (String[])map.keySet().toArray(new String[map.size()]);
for (int i = 0 ; i < keys.length; i++)
{
writer.write("\"" +keys[i]+"\"" + " 的字符有 " + map.get(keys[i])+" 个 " );
writer.write("\r\n" );
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.qunar.fresh.question1;
import java.io.*;
import java.util.*;
/**
* Created by deliang.gao on 2015/11/3.
*
* 思路:1.获取目录
* 2.读取文件
* 3.统计数量
* 4.写入文件
*
* */
public class Question1 {
public static int numCount=0 ;
public static int letterCount=0 ;
public static int chinaCount=0 ;
public static int sapceCount=0 ;
public static int lineCount=0 ;
public static Map numMap =new TreeMap();
public static Map letterMap =new TreeMap();
public static Map chineseMap =new TreeMap();
public static String line=null ;
public static Integer value=null ;
public static int word=0 ;
public static final String READPATH= Question1.class.getResource("/" ).getPath() +"files" ;
public static final String WRITEPATH=Question1.class.getResource("/" ).getPath() +"fileconfig" ;
public static void main (String args[]){
File readFile=new File(READPATH);
List<File> list=getAllFiles(readFile,new ArrayList<File>());
readFile(list);
writeFile();
}
public static List<File> getAllFiles (File dir,List<File> reFiles){
if (!dir.exists()||!dir.isDirectory()){
return null ;
}
File[] allFiles=dir.listFiles();
for (File file:allFiles){
if (file.isFile()){
reFiles.add(file);
}else if (file.isDirectory()){
getAllFiles(file,reFiles);
}
}
return reFiles;
}
public static void readFile (List<File> files){
BufferedReader reader=null ;
for (File file:files){
try {
reader=new BufferedReader(new FileReader(file));
while ((line=reader.readLine())!=null ){
lineCount++;
for (int i=0 ;i<line.length();i++){
line=line.toUpperCase();
word = line.charAt(i);
if ((word>='a' &&word<='z' )||(word>='A' &&word<='Z' )){
letterCount++;
readMap(line.charAt(i),letterMap);
}
else if (word>='0' &&word<='9' ){
numCount++;
readMap(line.charAt(i),numMap);
}
else if (word ==' ' ){
sapceCount++;
}
else {
chinaCount++;
readMap(line.charAt(i),chineseMap);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void readMap (char key0,Map<Character,Integer> map){
value=map.get(key0);
if (value==null ){
map.put(key0,new Integer(1 ));
}else {
map.put(key0,value+1 );
}
}
public static void writeFile (){
BufferedWriter writer=null ;
try {
writer=new BufferedWriter(new FileWriter(WRITEPATH));
writer.write("数字" + numCount + "个\n" );
writer.write("字母" +letterCount+"个\n" );
writer.write("汉字" +chinaCount+"个\n" );
writer.write("空格" +sapceCount+"个\n" );
writer.write("行数" +lineCount+"行\n" );
Iterator it1 = numMap.keySet().iterator();
while (it1.hasNext()) {
Object temp=it1.next();
writer.write("数字" + temp + ":" + numMap.get(temp));
writer.write("\r\n" );
}
Iterator it2 = letterMap.keySet().iterator();
while (it2.hasNext()) {
Object temp=it2.next();
writer.write("字母" + temp + ":" + letterMap.get(temp));
writer.write("\r\n" );
}
Iterator it3 = chineseMap.keySet().iterator();
while (it3.hasNext()) {
Object temp=it3.next();
writer.write("汉字" + temp + ":" + chineseMap.get(temp));
writer.write("\r\n" );
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}