csv 日志的写入与读取

来源:互联网 发布:excel表格数据统计分类 编辑:程序博客网 时间:2024/05/16 00:40


/**
 *
 * @author weij
 *
 */
public class LogDto {
 int idx;
 String logType;
 String logName;
 String dataTime;
 String owner;
 public String getLogType() {
  return logType;
 }
 public void setLogType(String logType) {
  this.logType = logType;
 }
 public String getLogName() {
  return logName;
 }
 public void setLogName(String logName) {
  this.logName = logName;
 }
 public String getDataTime() {
  return dataTime;
 }
 public void setDataTime(String dataTime) {
  this.dataTime = dataTime;
 }
 public String getOwner() {
  return owner;
 }
 public void setOwner(String owner) {
  this.owner = owner;
 }
 public int getIdx() {
  return idx;
 }
 public void setIdx(int idx) {
  this.idx = idx;
 }
}

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriteLog {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

 }
 private void writeLog(String fileName, LogDto dto)
 {  
  // 指定的目录下创建备份Log文件    
  FileWriter fileWriter = null;  
  try {   
   fileWriter = new FileWriter("d:/backup/Log_Detail.csv", true);  
   BufferedWriter write = new BufferedWriter(fileWriter);
   StringBuffer msg = new StringBuffer();
   msg.append(dto.getLogType()+ ",");
   msg.append(dto.getLogName() + ",");
   msg.append(dto.getDataTime() + ",");
   msg.append(dto.getOwner());

   write.write(msg.toString()); //写入日志内容
   write.newLine();
   
   write.flush();
   write.close();
   
  } catch (IOException e) {
  }finally{   
   try {    
    if(fileWriter != null){
     fileWriter.close();
    }
   } catch (IOException e) {
   }       
  }  
 }
}

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class GetLogDtoList {


 /**
  * 取得所有备份Oracle的日志信息
  * @return
  * @throws ServiceException
  */
 public List<LogDto> getDbBackupDtoList(){
  // TODO Auto-generated method stub
  List<LogDto> dtoList = new ArrayList<LogDto>();
  
  // 读取备份日志文件
  String sFileName = "d:/backup/DB_Backup_Detail.csv";
  FileReader fr = null;
  BufferedReader br = null;
  try {
   fr = new FileReader(sFileName);
   br = new BufferedReader(fr);
   String sRecord = null;
   int idx = 0;
   while ((sRecord = br.readLine()) != null) {
    
    String[] strArray = sRecord.split(",");
    if(strArray.length != 4){
     continue;
    }
    LogDto dto = new LogDto();
    dto.setIdx(++idx);
    dto.setLogType(strArray[0]);
    dto.setLogName(strArray[1]);
    dto.setDataTime(strArray[2]);    
    dto.setOwner(strArray[3]);    
    
    dtoList.add(dto);
   }
   
  } catch (FileNotFoundException e) {
  } catch (IOException e) {
  }finally{   
   try {
    if(br != null) br.close();
    if(fr != null) fr.close();
   } catch (IOException e) {
   }
  }
  return dtoList;
 }
}

原创粉丝点击