接到任务,让我统计一下有多少个用户id,咋一看,一大把密密麻麻的数字,但仔细一看,也不是无章可循的,经过几分钟的倒腾,终于弄出来了
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
public class Demo {
public static void main(String[] args) throws Exception {
String filePath = "src/login_2015-04-09.log";
int i = 1;
count(filePath, i);
}
private static void count(String filePath, int i) throws Exception {
String[] str = new String[1024];
try {
String encoding = "UTF-8";
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;
Set<String> ignoreChars = new HashSet<String>();
while ((lineTxt = bufferedReader.readLine()) != null) {
str = lineTxt.split(",");
System.out.println(str[1] + "=====");
ignoreChars.add(str[i]);
}
System.out.println(ignoreChars.size());
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
}