package service.spider.fileLocSys
import scala.io.Sourceimport java.io.{BufferedWriter, OutputStreamWriter, FileOutputStream, File}
/**
* Scala Io 基本文件读写
* author: w.j.w
* time: 15-12-11
* email: wujavei@163.com
*
*出其西门,有女如云,虽则如云,匪我思存。
*
*/
object FileLocService {
val docSaveDir:String="E:/WjWork/MIS/AliBabaAdSpider/spiderFile"
def readFile(file: String): String = {
//val doc = Source.fromFile(file)("UTF-8").mkString
val doc = Source.fromFile(file).mkString
println(doc)
doc
}
//write
def writeLocFile(code:String,encode:String):String={
//定义文件路径
val tpu=TimeUrlPathUtil
val timeFileName=System.currentTimeMillis()+""+ (Math.random()*1000).asInstanceOf[Int];
var filepath=this.docSaveDir+"/"+tpu.getTimePath()+"/"+timeFileName+".html"
val f=new File(filepath)
val fileParent=f.getParentFile
if(fileParent.exists()==false){
fileParent.mkdirs()
}
val fout=new FileOutputStream(f)
//指定输出文件编码
val ow=new OutputStreamWriter(fout,encode)
val bw=new BufferedWriter(ow)
bw.write(code)
bw.flush()
//关闭文件输出流
bw.close()
ow.close()
fout.close()
// println(filepath)
filepath
}
def main(args: Array[String]) {
val fcs = FileLocService
fcs.readFile("E:\\WjWork\\MIS\\AliBabaAdSpider\\1688spider_html_code.txt")
val path= fcs.writeLocFile("中文文档测试","utf-8")
println(path)
println("写入文件内容:"+fcs.readFile(path))
}
}
按时间定义文件相对路径
package service.spider.fileLocSys
import java.util.Date
import java.text.SimpleDateFormat
/**
* Created by Administrator on 15-12-11.
*/
object TimeUrlPathUtil {
def getTimePath(): String ={
val tp=getNowDate()
tp
}
def getNowDate():String={
var now:Date = new Date()
var dateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy/MM/dd")
var hehe = dateFormat.format( now )
hehe
}
}