package com.io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.io.object.Persion;
public class ReadFromFile {
/**
* @param args
*/
public static void main(String[] args) {
ReadFromFile.readFileByLine("D:\\workspace\\testFolder\\aaa.txt");
}
private static void readFileByLine(String name)
{
File file = new File(name);
BufferedReader reader = null;
Persion person = null;
try {
reader = new BufferedReader(new FileReader(file));
String str = null;
List<Persion> list = new ArrayList<Persion>();;
while ((str=reader.readLine())!=null) {
if(str.contains("name="))
{
String filename = str.substring(str.indexOf("=")+1, str.length());
person = new Persion();
person.setName(filename);
}
if(str.contains("age="))
{
String age = str.substring(str.indexOf("=")+1, str.length());
person.setAge(Integer.valueOf(age));
}
if(str.contains("sex="))
{
String sex = str.substring(str.indexOf("=")+1, str.length());
person.setSex(sex);
list.add(person);
}
}
Collections.sort(list, new Comparator<Persion>() {
@Override
public int compare(Persion o1, Persion o2) {
// TODO Auto-generated method stub
return String.valueOf(o1.getAge()).compareToIgnoreCase(String.valueOf(o2.getAge()));
}
});
for (Persion persion : list) {
System.out.println("name="+persion.getName());
System.out.println("age="+persion.getAge());
System.out.println("sex="+persion.getSex()+"\n");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
----------------
name=carter
age=26
sex=male
name=ugly
age=21
sex=female
name=forever
age=100
sex=unknow
分享到:
相关推荐
SAP best practise part 3
SAP Best Practise part1
CKS - Practise Immutable Resource
sap best practise 50097738_SAP_BP_Baseline_Package_v1-V2.603
practise-json 最新代码practise-json 最新代码practise-json 最新代码practise-json 最新代码practise-json 最新代码practise-json 最新代码practise-json 最新代码practise-json 最新代码practise-json 最新代码...
practise练习
j2ee best practise
PMP BOK and practise
这是关于db2 xml方面的最佳实践,仅供参考
practise3.cpp
Intergration Service Practise <2>
Practise questions for CKS
2008 CISA Practise Question
gwt in practise 教程,写的详细,非常适合入门初学者自学。
UIPATH证书考核,practise部分,一般uipath证书考试分为两部分,第一部分选择题,第二部分实操题,每个部分三次机会,题目随机,实操三题,都在附件中,可根据自己得题目找对应得代码
富士通出的ASM Best Practise
Netty 实战-netty-practise
CKS - Practise - NetPol
IBM TAM TIM step by step lab(从安装到配置 应用尽有) best practise
It's the practise questions for P1 exam from the offical website. very similar to the real test.