学习内容
1.File中的核心方法
2.File中的方法实例
3.网络IO
4.执行shell方法
5.线程,同步关键字
屏幕快照 2020-12-29 下午9.03.20.png
package day1
import java.io.BufferedReader
import java.io.File
import java.io.InputStream
import java.io.InputStreamReader
import java.net.URL
import java.nio.charset.Charset
import kotlin.concurrent.thread
/**
*
* IO操作
*/
fun main(args:Array<String>){
val path = ""
val f1 = getFileContent(path)
println("f1:"+f1)
val f2 = getFileLines(path)
println("f2:"+f2)
val f3 = getByteArray(path)
println("f3:"+f3)
joinJava(path)
writeFile("Hello world","file.data")
appendFile("Kotlin java","file.data")
travelFileTree("")
println(getUrlHTML("https://www.baidu.com"))
writeURLIntoFile("https://www.baidu.com","file.data")
writeURLIntoFile("https://t7.baidu.com/it/u=3065092861,1536340632&fm=193&f=GIF",
"1.jpg")
testProcess()
testThread()
}
@Volatile var running = false //kotlin没有volatile关键字
fun testThread(){
object :Thread(){
override fun run() {
Thread.sleep(1000)
println("A 使用Thread对象表达式:${Thread.currentThread()}")
}
}.start()
Thread({
Thread.sleep(1000)
println("A 使用Thread对象表达式:${Thread.currentThread()}")
}).start()
val t = Thread({
Thread.sleep(1000)
println("A 使用Thread对象表达式:${Thread.currentThread()}")
})
t.isDaemon = false
t.start()
thread(start = true,isDaemon = false) {
println("使用kotlin封装thread")
}
}
fun testProcess(){
val p = "ls -al".execute()
val exitCode = p.waitFor()
val text = p.text()
println(exitCode)
println(text)
}
fun Process.text():String{
var output = ""
val ins = this.inputStream
val isr = InputStreamReader(ins)
val reader = BufferedReader(isr)
var line:String? = ""
while (line!=null) {
line = reader.readLine()
output += line + "\n"
}
return output
}
fun String.execute():Process{
val runtime = Runtime.getRuntime()
return runtime.exec(this)
}
@Synchronized //同步标志
fun writeURLIntoFile(url: String,fileName: String){
val byte = URL(url).readBytes()
File(fileName).writeBytes(byte)
}
/**
* 获取响应的字节数组
*/
fun getURLBytes(url: String):ByteArray{
return URL(url).readBytes()
}
/**
* 根据URL获取URL响应内容HTML
*/
fun getUrlHTML(url:String):String{
return URL(url).readText(Charset.forName("UTF-8"))
}
fun getFileSequence(fileName: String,p:(File)->Boolean):Sequence<File>{
val f = File(fileName)
val fileWalk = f.walk()
return fileWalk.filter(p)
}
fun getFileIterator(fileName: String):Iterator<File>{
val f = File(fileName)
val fileWalk = f.walk()
return fileWalk.iterator()
}
fun travelFileTree(fileName: String){
val f = File(fileName)
val fileWalk = f.walk()
fileWalk.iterator().forEach {
println(it.absolutePath)
}
}
fun appendFile(text:String, destFile:String){
var f = File(destFile)
if (!f.exists()) {
f.createNewFile()
}
f.appendText(text, Charset.defaultCharset())
}
fun writeFile(text:String, destFile:String){
var f = File(destFile)
if (!f.exists()) {
f.createNewFile()
}
f.writeText(text,Charset.defaultCharset())
}
fun getFileContent(fileName:String):String{
var f = File(fileName)
return f.readText(Charset.forName("UTF-8"))
}
fun getFileLines(fileName: String):List<String>{
return File(fileName).readLines(Charset.forName("UTF-8"));
}
fun getByteArray(fileName: String):ByteArray{
return File(fileName).readBytes();
}
fun joinJava(fileName: String){
val reader = File(fileName).reader()
val ins:InputStream = File(fileName).inputStream()
val bfd :BufferedReader = File(fileName).bufferedReader()
}