文章目录
一、基本任务:(实现代码包括了三个需要编写的方法)1.先创建一个Log类2.实现代码
二、贯穿任务(物流管理系统)任务1-1升级实体类为可序列化的类,以便在文件中保存或网络中传递。DataBase.javaLogRec.javaMatchedLogRec.javaTransport.javaMatchedTransport.java
任务1-2 实现匹配的日志信息的保存和读取功能。LogRecService.java
任务1-3 实现匹配的物流信息的保存和读取功能。TransportService.java
任务1-4 测试匹配的日志、物流信息的保存和读取功能。FileDemo.java
实训一下载地址:https://picture.hs-vae.com/practice1.zip
里面包含了com.qst.dms包,log.txt文件,以及实验报告 个人博客里面有其他java的知识,欢迎访问哦
一、基本任务:(实现代码包括了三个需要编写的方法)
提醒一下:我写class的属于vae项目里面,我这里为了使用相对路径简便,把log.txt文件放在vae项目里,这样在写路径的时候可以直接写"log.txt"
完成如下图所示log.txt日志文件的分析,编写方法分析访问量排名前10的IP地址打印输出。
编写方法读取日志文件分析出有哪几种不同的错误异常。
3)编写方法完成日志文件的字段对象封装,完成对象的序列化。
1.先创建一个Log类
package Demo5Experiment
.Demo1
;
import java
.io
.Serializable
;
public class Log implements Serializable {
private static final long serialVersiouUID
=1L
;
private String name
;
private String time
;
private String Ip
;
private String status
;
private String information
;
public String
getName() {
return name
;
}
@Override
public String
toString() {
return "Log{" +
"name='" + name
+ '\'' +
", time='" + time
+ '\'' +
", Ip='" + Ip
+ '\'' +
", status='" + status
+ '\'' +
", information='" + information
+ '\'' +
'}';
}
public Log(String name
, String time
, String Ip
, String status
, String information
) {
this.name
= name
;
this.time
=time
;
this.Ip
=Ip
;
this.status
=status
;
this.information
=information
;
}
public Log(String s
) {
}
public String
getIp() {
return Ip
;
}
public String
getInformation() {
return information
;
}
public void setInformation(String information
) {
this.information
= information
;
}
public String
getStatus() {
return status
;
}
public void setStatus(String status
) {
this.status
= status
;
}
public void setIp(String ip
) {
Ip
= ip
;
}
public String
getTime() {
return time
;
}
public void setTime(String time
) {
this.time
= time
;
}
public void setName(String name
) {
this.name
= name
;
}
}
2.实现代码
package Demo5Experiment
.Demo1
;
import java
.io
.*
;
import java
.util
.*
;
public class Demo1Experiment {
public static void main(String
[] args
) throws IOException
, ClassNotFoundException
{
printIp(new FileReader("log.txt"));
printError(new FileReader("log.txt"));
objectLog(new FileReader("log.txt"),new FileOutputStream("objectlog.txt"),new FileInputStream("objectlog.txt"));
}
private static void printIp(FileReader file
) throws IOException
{
BufferedReader br
= new BufferedReader(file
);
String line
;
System
.out
.println("访问量排名前十的IP地址:");
for (int i
= 0; i
< 10; i
++) {
line
= br
.readLine();
String
[] arr
=line
.split(",",5);
System
.out
.println(arr
[2]);
}
br
.close();
}
private static void printError(FileReader file
) throws IOException
{
BufferedReader br
= new BufferedReader(file
);
ArrayList
<String> list
=new ArrayList<>();
Set
<String> set
=new HashSet<String>();
String line
;
int i
=0;
while ((line
= br
.readLine()) != null
) {
String
[] arr
= line
.split("\\,",5);
list
.add(arr
[3]);
if(list
.get(i
).trim().equals("ERROR")){
set
.add(arr
[4]);
}
++i
;
}
System
.out
.println("错误异常信息:");
for(String s
:set
){
System
.out
.println(s
);
}
br
.close();
}
private static void objectLog(FileReader file1
,FileOutputStream file2
,FileInputStream file3
) throws IOException
, ClassNotFoundException
{
ObjectOutputStream oos
=new ObjectOutputStream(file2
);
BufferedReader br
=new BufferedReader(file1
);
ObjectInputStream ois
= new ObjectInputStream(file3
);
ArrayList
<Log> list
=new ArrayList<>();
String line
;
while ((line
= br
.readLine()) != null
) {
String
[] arr
= line
.split(",",5);
list
.add(new Log(arr
[0],arr
[1],arr
[2],arr
[3],arr
[4]));
}
for (int i
= 0; i
< list
.size(); i
++) {
oos
.writeObject(list
);
}
Object o
=ois
.readObject();
ArrayList
<Log> a
=(ArrayList
<Log>)o
;
System
.out
.println("序列化后:");
for (Log p
:a
){
System
.out
.println(p
);
}
oos
.close();
br
.close();
ois
.close();
}
}
二、贯穿任务(物流管理系统)
【任务1-1】 升级实体类为可序列化的类,以便在文件中保存或网络中传递。
DataBase.java
LogRec.java
MatchedLogRec.java
Transport.java
MatchedTransport.java
【任务1-2】 实现匹配的日志信息的保存和读取功能。
LogRecService.java
【任务1-3】 实现匹配的物流信息的保存和读取功能。
TransportService.java
【任务1-4】测试匹配的日志、物流信息的保存和读取功能。
FileDemo.java
任务1-1升级实体类为可序列化的类,以便在文件中保存或网络中传递。
DataBase.java
package com
.qst
.dms
.entity
;
import java
.io
.Serializable
;
import java
.util
.Date
;
public class DataBase implements Serializable{
private int id
;
private Date time
;
private String address
;
private int type
;
public static final int GATHER
=1;
public static final int MATHCH
=2;
public static final int RECORD
=3;
public static final int SEND
=4;
public static final int RECIVE
=5;
public static final int WRITE
=6;
public static final int SAVE
=7;
public int getId() {
return id
;
}
public void setId(int id
) {
this.id
= id
;
}
public Date
getTime() {
return time
;
}
public void setTime(Date time
) {
this.time
= time
;
}
public String
getAddress() {
return address
;
}
public void setAddress(String address
) {
this.address
= address
;
}
public int getType() {
return type
;
}
public void setType(int type
) {
this.type
= type
;
}
public DataBase() {
}
public DataBase(int id
, Date time
, String address
, int type
) {
this.id
= id
;
this.time
= time
;
this.address
= address
;
this.type
= type
;
}
public String
toString() {
return id
+ "," + time
+ "," + address
+ "," + type
;
}
}
LogRec.java
package com
.qst
.dms
.entity
;
import java
.io
.Serializable
;
import java
.util
.Date
;
public class LogRec extends DataBase implements Serializable{
private String user
;
private String ip
;
private int logType
;
public static final int LOG_IN
=1;
public static final int LOG_OUT
=0;
public String
getUser() {
return user
;
}
public void setUser(String user
) {
this.user
= user
;
}
public String
getIp() {
return ip
;
}
public void setIp(String ip
) {
this.ip
= ip
;
}
public int getLogType() {
return logType
;
}
public void setLogType(int logType
) {
this.logType
= logType
;
}
public LogRec() {
}
public LogRec(int id
, Date time
, String address
, int type
,String user
,String ip
,int logType
) {
super(id
,time
,address
,type
);
this.user
=user
;
this.ip
=ip
;
this.logType
=logType
;
}
public String
toString() {
return this.getId() + "," +this.getTime() + "," +this.getAddress() + "," + this.getType() + ","+user
+","+ip
+","+logType
;
}
}
MatchedLogRec.java
package com
.qst
.dms
.entity
;
import java
.io
.Serializable
;
import java
.util
.Date
;
public class MatchedLogRec implements Serializable{
private LogRec login
;
private LogRec logout
;
public String
getUser() {
return login
.getUser();
}
public Date
getLogInTime() {
return login
.getTime();
}
public Date
getLogoutTime() {
return logout
.getTime();
}
public LogRec
getLogin() {
return login
;
}
public LogRec
getLogout() {
return logout
;
}
public MatchedLogRec() {
}
public MatchedLogRec(LogRec login
, LogRec logout
) {
if (login
.getLogType() != LogRec
.LOG_IN
) {
throw new RuntimeException("不是登录记录!");
}
if (logout
.getLogType() != LogRec
.LOG_OUT
) {
throw new RuntimeException("不是登出记录");
}
if (!login
.getUser().equals(logout
.getUser())) {
throw new RuntimeException("登录登出必须是同一个用户!");
}
if (!login
.getIp().equals(logout
.getIp())) {
throw new RuntimeException("登录登出必须是同一个IP地址!");
}
this.login
= login
;
this.logout
= logout
;
}
public String
toString() {
return login
.toString() + " | " + logout
.toString();
}
}
Transport.java
package com
.qst
.dms
.entity
;
import java
.io
.Serializable
;
import java
.util
.Date
;
public class Transport extends DataBase implements Serializable{
private String handler
;
private String reciver
;
private int transportType
;
public static final int SENDDING
= 1;
public static final int TRANSPORTING
= 2;
public static final int RECIEVED
= 3;
public String
getHandler() {
return handler
;
}
public void setHandler(String handler
) {
this.handler
= handler
;
}
public String
getReciver() {
return reciver
;
}
public void setReciver(String reciver
) {
this.reciver
= reciver
;
}
public int getTransportType() {
return transportType
;
}
public void setTransportType(int transportType
) {
this.transportType
= transportType
;
}
public Transport() {
}
public Transport(int id
, Date time
, String address
, int type
,
String handler
, String reciver
, int transportType
) {
super(id
, time
, address
, type
);
this.handler
= handler
;
this.reciver
= reciver
;
this.transportType
= transportType
;
}
public String
toString() {
return this.getId() + "," + this.getTime() + "," + this.getAddress()
+ "," + this.getType() + "," + handler
+ "," + transportType
;
}
}
MatchedTransport.java
package com
.qst
.dms
.entity
;
import java
.io
.Serializable
;
public class MatchedTransport implements Serializable{
private Transport send
;
private Transport trans
;
private Transport receive
;
public Transport
getSend() {
return send
;
}
public void setSend(Transport send
) {
this.send
= send
;
}
public Transport
getTrans() {
return trans
;
}
public void setTrans(Transport trans
) {
this.trans
= trans
;
}
public Transport
getReceive() {
return receive
;
}
public void setReceive(Transport receive
) {
this.receive
= receive
;
}
public MatchedTransport() {
}
public MatchedTransport(Transport send
, Transport trans
, Transport receive
) {
if (send
.getTransportType() != Transport
.SENDDING
) {
throw new RuntimeException("不是发货记录!");
}
if (trans
.getTransportType() != Transport
.TRANSPORTING
) {
throw new RuntimeException("不是送货记录!");
}
if (receive
.getTransportType() != Transport
.RECIEVED
) {
throw new RuntimeException("不是签收记录!");
}
this.send
= send
;
this.trans
= trans
;
this.receive
= receive
;
}
public String
toString() {
return send
.toString() + "|" + trans
.toString() + "|" + receive
;
}
}
任务1-2 实现匹配的日志信息的保存和读取功能。
LogRecService.java
package com
.qst
.dms
.service
;
import java
.io
.*
;
import java
.util
.ArrayList
;
import java
.util
.Date
;
import java
.util
.Scanner
;
import com
.qst
.dms
.entity
.DataBase
;
import com
.qst
.dms
.entity
.LogRec
;
import com
.qst
.dms
.entity
.MatchedLogRec
;
public class LogRecService {
public LogRec
inputLog() {
LogRec log
= null
;
Scanner scanner
= new Scanner(System
.in
);
try {
System
.out
.println("请输入ID标识: ");
int id
= scanner
.nextInt();
Date nowDate
= new Date();
System
.out
.println("请输入地址:");
String address
= scanner
.next();
int type
= DataBase
.GATHER
;
System
.out
.println("请输入 登录用户名:");
String user
= scanner
.next();
System
.out
.println("请输入 主机IP:");
String ip
= scanner
.next();
System
.out
.println("请输入登录状态:1是登录,0是登出");
int logType
= scanner
.nextInt();
log
= new LogRec(id
, nowDate
, address
, type
, user
, ip
, logType
);
} catch (Exception e
) {
System
.out
.println("采集的日志信息不合法");
}
return log
;
}
public void showLog(LogRec
... logRecs
) {
for (LogRec e
: logRecs
) {
if (e
!= null
) {
System
.out
.println(e
.toString());
}
}
}
public void showMatchLog(MatchedLogRec
... matchLogs
) {
for (MatchedLogRec e
: matchLogs
) {
if (e
!= null
) {
System
.out
.println(e
.toString());
}
}
}
public void showMatchLog(ArrayList
<MatchedLogRec> matchLogs
) {
for (MatchedLogRec e
: matchLogs
) {
if (e
!= null
) {
System
.out
.println(e
.toString());
}
}
}
public void saveMatchLog(ArrayList
<MatchedLogRec> matchLogs
) throws IOException
, ClassNotFoundException
{
ObjectOutputStream oos
=new ObjectOutputStream(new FileOutputStream("matchlog.txt"));
for(MatchedLogRec e
:matchLogs
){
if (e
!=null
){
oos
.writeObject(e
);
}
}
oos
.writeObject(null
);
oos
.flush();
oos
.close();
}
public ArrayList
<MatchedLogRec> readMatchLog() throws IOException
, ClassNotFoundException
{
ArrayList
<MatchedLogRec> matchLogs
=new ArrayList<>();
ObjectInputStream ois
=new ObjectInputStream(new FileInputStream("matchlog.txt"));
MatchedLogRec matchLog
;
while ((matchLog
=(MatchedLogRec
)ois
.readObject())!=null
){
matchLogs
.add(matchLog
);
}
ois
.close();
return matchLogs
;
}
}
任务1-3 实现匹配的物流信息的保存和读取功能。
TransportService.java
package com
.qst
.dms
.service
;
import java
.io
.*
;
import java
.util
.ArrayList
;
import java
.util
.Date
;
import java
.util
.Scanner
;
import com
.qst
.dms
.entity
.DataBase
;
import com
.qst
.dms
.entity
.MatchedLogRec
;
import com
.qst
.dms
.entity
.MatchedTransport
;
import com
.qst
.dms
.entity
.Transport
;
public class TransportService {
public Transport
inputTransport() {
Transport trans
= null
;
Scanner scanner
= new Scanner(System
.in
);
try{
System
.out
.println("请输入ID标识:");
int id
= scanner
.nextInt();
Date nowDate
= new Date();
System
.out
.println("请输入地址:");
String address
= scanner
.next();
int type
= DataBase
.GATHER
;
System
.out
.println("请输入货物经手人:");
String handler
= scanner
.next();
System
.out
.println("请输入 收货人:");
String reciver
= scanner
.next();
System
.out
.println("请输入物流状态:1发货中,2送货中,3已签收");
int transportType
= scanner
.nextInt();
trans
= new Transport(id
, nowDate
, address
, type
, handler
, reciver
,
transportType
);
} catch (Exception e
) {
System
.out
.println("采集的日志信息不合法");
}
return trans
;
}
public void showTransport(Transport
... transports
) {
for (Transport e
: transports
) {
if (e
!= null
) {
System
.out
.println(e
.toString());
}
}
}
public void showMatchTransport(MatchedTransport
... matchTrans
) {
for (MatchedTransport e
: matchTrans
) {
if (e
!= null
) {
System
.out
.println(e
.toString());
}
}
}
public void showMatchTransport(ArrayList
<MatchedTransport> matchTrans
) {
for (MatchedTransport e
: matchTrans
) {
if (e
!= null
) {
System
.out
.println(e
.toString());
}
}
}
public void saveMatchedTransport(ArrayList
<MatchedTransport> matchTrans
) throws IOException
{
ObjectOutputStream oos
=new ObjectOutputStream(new FileOutputStream("matchtrans.txt"));
for (MatchedTransport e
:matchTrans
) {
if(e
!=null
){
oos
.writeObject(e
);
}
}
oos
.writeObject(null
);
oos
.flush();
oos
.close();
}
public ArrayList
<MatchedTransport> readMatchedTransport() throws IOException
, ClassNotFoundException
{
ArrayList
<MatchedTransport> matchTrans
=new ArrayList<>();
ObjectInputStream ois
=new ObjectInputStream(new FileInputStream("matchtrans.txt"));
MatchedTransport matchTran
;
while ((matchTran
=(MatchedTransport
) ois
.readObject())!=null
){
matchTrans
.add(matchTran
);
}
ois
.close();
return matchTrans
;
}
}
任务1-4 测试匹配的日志、物流信息的保存和读取功能。
FileDemo.java
package com
.qst
.dms
.dos
;
import java
.io
.*
;
import java
.util
.ArrayList
;
import java
.util
.Date
;
import com
.qst
.dms
.entity
.DataBase
;
import com
.qst
.dms
.entity
.LogRec
;
import com
.qst
.dms
.entity
.MatchedLogRec
;
import com
.qst
.dms
.entity
.MatchedTransport
;
import com
.qst
.dms
.entity
.Transport
;
import com
.qst
.dms
.service
.LogRecService
;
import com
.qst
.dms
.service
.TransportService
;
public class FileDemo {
public static void main(String
[] args
) throws IOException
, ClassNotFoundException
{
LogRecService logService
= new LogRecService();
ArrayList
<MatchedLogRec> matchLogs
= new ArrayList<>();
matchLogs
.add(new MatchedLogRec(
new LogRec(1001, new Date(), "青島",DataBase
.GATHER
, "zhangsan", "192.168.1.1", 1),
new LogRec(1002, new Date(), "青島", DataBase
.GATHER
, "zhangsan", "192.168.1.1", 0)));
matchLogs
.add(new MatchedLogRec(
new LogRec(1003, new Date(), "北京",DataBase
.GATHER
, "lisi", "192.168.1.6", 1),
new LogRec(1004, new Date(), "北京", DataBase
.GATHER
, "lisi", "192.168.1.6", 0)));
matchLogs
.add(new MatchedLogRec(
new LogRec(1005, new Date(), "济南",DataBase
.GATHER
, "wangwu", "192.168.1.89", 1),
new LogRec(1006, new Date(), "济南", DataBase
.GATHER
, "wangwu", "192.168.1.89", 0)));
logService
.saveMatchLog(matchLogs
);
ArrayList
<MatchedLogRec> logList
=logService
.readMatchLog();
System
.out
.println("-------------------------日志信息-------------------------");
logService
.showMatchLog(logList
);
TransportService tranService
= new TransportService();
ArrayList
<MatchedTransport> matchTrans
= new ArrayList<>();
matchTrans
.add(new MatchedTransport(
new Transport(2001, new Date(), "青島",DataBase
.GATHER
,"zhangsan","zhaokel",1),
new Transport(2002, new Date(), "北京",DataBase
.GATHER
,"lisi","zhaokel",2),
new Transport(2003, new Date(), "北京",DataBase
.GATHER
,"wangwu","zhaokel",3)));
matchTrans
.add(new MatchedTransport(
new Transport(2004, new Date(), "青島",DataBase
.GATHER
,"maliu","zhaokel",1),
new Transport(2005, new Date(), "北京",DataBase
.GATHER
,"sunqi","zhaokel",2),
new Transport(2006, new Date(), "北京",DataBase
.GATHER
,"fengba","zhaokel",3)));
tranService
.saveMatchedTransport(matchTrans
);
ArrayList
<MatchedTransport> tranList
=tranService
.readMatchedTransport();
System
.out
.println("--------------------------物流信息------------------------");
tranService
.showMatchTransport(tranList
);
}
}