somoly.tistory.com
Published 2023. 9. 9. 21:57
kotlin euc-kr to utf-8 conversion Kotlin

euc-kr 로 된 자막파일을 utf-8 로 변환하기 위해 사용한 것

import java.nio.charset.Charset
import java.nio.file.Files
import kotlin.io.path.Path
import kotlin.io.path.extension
import kotlin.io.path.readBytes

fun main() {
	// 해당 경로의 파일 목록을 가져오기
	val stream = Files.list(Path("path/files"))

	// smi 파일들만 필터링
	stream.filter { it.extension == "smi" }
		.forEach {
			// 파일을 byte array 로 변환
			val bytes = it.readBytes()
            
			// string 으로 변환 (euc-kr)
			val euckr = String(bytes, Charset.forName("euc-kr"))
            
			// tmp 경로에 utf-8 로 변환하여 저장
			Files.write(Path("/tmp/files/${it.fileName}"), euckr.toByteArray(Charsets.UTF_8))
		}

}

 

'Kotlin' 카테고리의 다른 글

Kotlin Coroutine 개념  (0) 2023.10.05
Apache POI sheet.autoSizeColumn() Speed Up  (0) 2023.08.08
kotlin logger  (0) 2023.06.26
Kotlin Collections overview  (0) 2023.03.26
profile

somoly.tistory.com

@RxCats

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!