package com.example.myapplication
import org.junit.Test
import org.junit.Assert.*
import java.io.File
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
@Test
fun readText() {
val filename = """D:\Document\WorkRelated\2020\11\09\tmp.txt"""
val file = File(filename)
val contents = file.readText()
println(contents)
file.readLines().take(3).forEach {
println(it.toUpperCase())
}
file.forEachLine(action = ::println)
val bytes: ByteArray = file.readBytes()
println(bytes.joinToString(separator = ""))
val reader = file.reader()
val inputStream = file.inputStream()
val bufferedReader = file.bufferedReader()
}
@Test
fun writeFile() {
val currentDir = """D:\Document\WorkRelated\2020\11\09"""
val file = File(currentDir, "out.txt")
if (file.exists()) {
file.createNewFile()
}
val writer = file.bufferedWriter()
writer.append("ddd")
writer.close()
}
@Test
fun traverseFileTree() {
val dir = File("""D:\Document\WorkRelated\2020\11\09""")
val fileTree = dir.walk()
fileTree.maxDepth(1)
.filter { it.isFile }
.filter { it.extension == "txt" }
.forEach(::println)
}
@Test
fun regex() {
val source = """ddfdf<p class="reader-word-layer reader-word-s1-3 reader-word-s1-4" style="width: 1339px; height: 222px; line-height: 222px; top: 1332px; left: 1764px; z-index: 2; font-weight: 600; letter-spacing: 0.33px;">墙面一般抹灰</p>ddd<pddd>ddd</p>3434"""
val pattern = """.*?<p.*?>(.*?)</p>.*?"""
Regex(pattern)
.findAll(source)
.toList()
.flatMap(MatchResult::groupValues)
.forEach(::println)
}
@Test
fun crack() {
val filename = """D:\Document\WorkRelated\2020\11\09\tmp.txt"""
val file = File(filename)
val bufferedReader = file.bufferedReader(Charsets.UTF_8)
val stringBuilder = StringBuilder("")
bufferedReader.lines()
.forEach{
stringBuilder.append(it)
}
val source = stringBuilder.toString()
println(source)
val pattern = """.*?<p.*?>(.*?)</p>.*?"""
val currentDir = """D:\Document\WorkRelated\2020\11\09"""
val newFile = File(currentDir, "out.txt")
if (newFile.exists()) {
newFile.createNewFile()
}
val writer = newFile.bufferedWriter()
Regex(pattern)
.findAll(source)
.toList()
.flatMap(MatchResult::groupValues)
.filter {
!it.contains("</p>")
}
.forEach {
println(it)
writer.append(it)
}
writer.close()
}
}