java读取文本文件做简单处理

来源:互联网 发布:环法自行车价格 知乎 编辑:程序博客网 时间:2024/05/16 08:40

我现在有一些文本文件,具有固定行格式,但是其中有一些行不符合固定格式,我需要利用正则表达式将这些去掉,格式date$time$ip$url scala编程

import java.io._import scala.util.matching.Regexobject delect {  def main(args: Array[String]): Unit = {    val filein="H:\\IISWebData\\LogResult.txt"    val fileout="H:\\IISWebData\\LogResultD.txt"    val infile=new File(filein)    val fr=new FileReader(infile)    val in=new BufferedReader(fr)    val outfile=new File(fileout)    if(!outfile.exists()){      val writer=new PrintWriter(outfile)      writer.close()    }    val fw = new FileWriter(outfile,true)    val out = new BufferedWriter(fw)try{      var s:String = null    while((s=in.readLine())!=null){      if(CheckLogData(s)){        out.write(s)        out.newLine()      }    }}    catch {      case ex:Throwable=>println("1")    }    out.close()  }  def CheckLogData(line:String):Boolean={    val Parttern:Regex="""^(\d{4}-\d{1,2}-\d{1,2})\$(\d{2}:\d{2}:\d{2})\$(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\$(.*?\.aspx|.*?\.html)$""".r    val option=Parttern.findFirstMatchIn(line)    if (option.isEmpty){      false    }else{      true    }  }}