java从txt中读取数据到数组,并对EnNo属性进行排序
数据为:EnNoNameGMNoModeDoorIN/OUTAntipassDateTime00000061110S02014-10-3109:28:0300000038...
数据为:
EnNo Name GMNo Mode Door IN/OUT Antipass DateTime
00000061 1 1 0 S 0 2014-10-31 09:28:03
00000038 1 1 0 S 0 2014-10-31 09:39:08
00000070 1 1 0 E 0 2014-10-31 17:10:05
00000022 1 1 0 E 0 2014-10-31 17:58:38
00000040 1 1 0 E 0 2014-10-31 18:00:25
00000038 1 1 0 E 0 2014-10-31 18:00:26
00000018 1 1 0 E 0 2014-10-31 18:00:29
00000018 1 1 0 E 0 2014-10-31 18:00:31 展开
EnNo Name GMNo Mode Door IN/OUT Antipass DateTime
00000061 1 1 0 S 0 2014-10-31 09:28:03
00000038 1 1 0 S 0 2014-10-31 09:39:08
00000070 1 1 0 E 0 2014-10-31 17:10:05
00000022 1 1 0 E 0 2014-10-31 17:58:38
00000040 1 1 0 E 0 2014-10-31 18:00:25
00000038 1 1 0 E 0 2014-10-31 18:00:26
00000018 1 1 0 E 0 2014-10-31 18:00:29
00000018 1 1 0 E 0 2014-10-31 18:00:31 展开
展开全部
package p1;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Guy
{
private String enNo;
private String name;
private String gMNo;
private String mode;
private String doorIN_OUT;
private String antipass;
private String dateTime;
public Guy ( String enNo, String name, String gMNo, String mode, String doorIN_OUT, String antipass, String dateTime )
{
this.enNo = enNo;
this.name = name;
this.gMNo = gMNo;
this.mode = mode;
this.doorIN_OUT = doorIN_OUT;
this.antipass = antipass;
this.dateTime = dateTime;
}
@Override
public String toString ()
{
StringBuilder builder = new StringBuilder ();
builder.append ("Guy [enNo=").append (enNo).append (", name=").append (name).append (", gMNo=").append (gMNo)
.append (", mode=").append (mode).append (", doorIN_OUT=").append (doorIN_OUT).append (", antipass=")
.append (antipass).append (", dateTime=").append (dateTime).append ("]");
return builder.toString ();
}
private static final String FILE = "guy.txt";
public static void main ( String[] args )
{
Guy[] guys = new Guy[0];
Scanner scanner = null;
try
{
scanner = new Scanner (new File (FILE));
String line = null;
while (scanner.hasNextLine ())
{
line = scanner.nextLine ().trim ();
if (line.startsWith ("EnNo"))
{
continue;
}
String[] info = line.split ("\\s+");
Guy guy = new Guy (info[0], info[1], info[2], info[3], info[4], info[5], info[6] + info[7]);
Guy[] temp = new Guy[guys.length + 1];
System.arraycopy (guys, 0, temp, 0, guys.length);
temp[temp.length - 1] = guy;
guys = temp;
}
Arrays.sort (guys, new Comparator<Guy> ()
{
@Override
public int compare ( Guy o1, Guy o2 )
{
return o1.enNo.compareTo (o2.enNo);
}
});
for ( Guy guy : guys )
{
System.out.println (guy);
}
}
catch (FileNotFoundException e)
{
e.printStackTrace ();
}
finally
{
if (null != scanner)
{
scanner.close ();
}
}
}
}
展开全部
你可以把EnNo及相关的数组放在TreeMap中,EnNo 为key, dataArray为Value, 得到的TreeMap的Values 就是你所要排序后的结果
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你数组里放的是一个对象还是用个二维数组保存的
追问
数组保存。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询