【Java】员工统计
1.定义个文档“员工.txt”,里面的内容为:
李文,北京
李希,北京
张小小,河南
王文,河北
董信,北京
张坤,北京
万三,江西
戴天,河南
庞大,湖北
彭克,浙江
生成另外一个文档“员工统计.txt”中,其格式为:
北京,4(董信,李文,李希,张坤)
河南,2(戴天,张小小)
河北,1(王文)
江西,1(万三)
湖北,1(庞大)
浙江,1(彭克)
使用类集方法
使用List接口
package chapter13;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class Job {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String file1 = "d:/员工.txt";
File f1 = new File(file1);
FileReader in = new FileReader(f1);
BufferedReader breader = new BufferedReader(in);
String aLine;
ArrayList<String> listProvince = new ArrayList<String>();
ArrayList<Integer> listNumber = new ArrayList<Integer>();
ArrayList<String> listDetail = new ArrayList<String>();
while ((aLine = breader.readLine()) != null)
{
String name = aLine.substring(0, aLine.indexOf(",")); //取出从0到”,“变成子字符串
String province = aLine.substring(aLine.indexOf("," ) + 1);//取出”,“以后的所有子字符串
int pos = 0;
boolean find = false;
for (int i = 0; i < listProvince.size(); i++)
{
if (province.equals(listProvince.get(i)))
{
pos = i;
find = true;
break;
}
}
if (find)
{
listNumber.set(pos, (int)listNumber.get(pos) + 1);
listDetail.set(pos, listDetail.get(pos) + "," + name);
}
else
{
listProvince.add(province);
listNumber.add(1);
listDetail.add(name);
}
}
breader.close();
String file2 = "d:/员工统计.txt";
File f2 = new File(file2);
if(f2.exists())
f2.delete();
FileWriter writerStream = new FileWriter(file2);
BufferedWriter bwriter = new BufferedWriter(writerStream);
for(int i = 0; i < listProvince.size(); i++)
{
aLine = listProvince.get(i) + "," + listNumber.get(i) + "(" + listDetail.get(i) + ")\r\n";
bwriter.write(aLine);
}
bwriter.flush();
bwriter.close();
}
}
使用Map接口
package chapter13;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Job2 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String file = "d:/员工.txt";
FileReader in = new FileReader(file);
BufferedReader breader = new BufferedReader(in);
Map<String, String> map = new HashMap<String, String>();
String aLine;
while((aLine = breader.readLine()) != null)
{
String name = aLine.substring(0, aLine.indexOf(","));
String province = aLine.substring(aLine.indexOf(",") + 1);
if(map.get(province) != null)
{
map.replace(province, map.get(province) + "," + name);
}
else
map.put(province, name);
breader.close();
Set<String> keys = map.keySet();
Iterator<String> iter = keys.iterator();
String file2 = "d:/员工统计.txt";
File f = new File(file2);
if(f.exists())
f.delete();
FileWriter writerStream = new FileWriter(file2);
BufferedWriter bwriter = new BufferedWriter(writerStream);
while(iter.hasNext())
{
String str = iter.next();
String[] split = map.get(str).split(","); //把一个字符串分隔成字符串数组,分隔符为”,“
bwriter.write(str + "," + split.length + "(" + map.get(str) + ")\r\n"); //获取分隔数组长度
}
bwriter.flush();
bwriter.close();
}
}
}
请你务必,一而再,再而三,三而不竭,千次万次,毫不犹豫地救自己于这世间水火。
笔者将不定期更新【考研或就业】的专业相关知识以及自身理解,希望大家能【关注】我。
如果觉得对您有用,请点击左下角的【点赞】按钮,给我一些鼓励,谢谢!
如果有更好的理解或建议,请在【评论】中写出,我会及时修改,谢谢啦!
笔者将不定期更新【考研或就业】的专业相关知识以及自身理解,希望大家能【关注】我。
如果觉得对您有用,请点击左下角的【点赞】按钮,给我一些鼓励,谢谢!
如果有更好的理解或建议,请在【评论】中写出,我会及时修改,谢谢啦!
本文来自博客园,作者:Nemo&
转载请注明原文链接:https://www.cnblogs.com/blknemo/p/10021003.html
顶
关注
评论
收藏