-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.kt
115 lines (103 loc) · 4.11 KB
/
Helper.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.io.*
import java.nio.charset.Charset
import java.util.Scanner
object Helper {
private const val BASE_PATH = "../resources/"
/**
* Receive the path of a file and copy it to other path
*/
fun copyFile(basePath: String = BASE_PATH,
sourcePath: String, destinationPath: String){
try {
val sourceFile = File(basePath + sourcePath)
val destinationFile = File(basePath + destinationPath)
FileInputStream(sourceFile).use { fileInputStream ->
FileOutputStream(destinationFile).use { fileOutputStream ->
var byte: Int
while (fileInputStream.read().also { byte = it } != -1) {
fileOutputStream.write(byte)
}
}
}
} catch(exception: Exception) {
println(exception)
}
}
/**
* Receive the path of a file and copy it to other path consider the size of the block of bytes during this process
*/
fun copyFileByBlockSize(basePath: String = BASE_PATH,
sourcePath: String, destinationPath: String, blockSize: Int = 8192
){
try {
val sourceFile = File(basePath + sourcePath)
val destinationFile = File(basePath + destinationPath)
FileInputStream(sourceFile).use { fileInputStream ->
FileOutputStream(destinationFile).use { fileOutputStream ->
val buffer = ByteArray(blockSize)
var len: Int
while (fileInputStream.read(buffer).also { len = it } != -1) {
fileOutputStream.write(buffer, 0, len)
}
}
}
} catch (exception: Exception){
println(exception)
}
}
/**
* Receive the path of a file in the ISO-8859-1 codification and convert it to UTF-8
*/
fun readAsIsoAndConvertToUTF(basePath: String = BASE_PATH,
sourcePath: String, destinationPath: String, blockSize: Int = 1024)
{
try {
val sourceFile = File(basePath + sourcePath)
val destinationFile = File(basePath + destinationPath)
FileInputStream(sourceFile).use { fileInputStream ->
InputStreamReader(fileInputStream, Charset.forName("ISO-8859-1")).use { inputStreamReader ->
FileOutputStream(destinationFile).use { fileOutputStream ->
OutputStreamWriter(fileOutputStream, Charset.forName("UTF-8")).use { outputStreamReader ->
val buffer = CharArray(blockSize)
var charsRead: Int
while (inputStreamReader.read(buffer).also { charsRead = it } != -1) {
outputStreamReader.write(buffer, 0, charsRead)
}
}
}
}
}
} catch (exception: Exception){
println(exception)
}
}
/**
* Read the keyboard input from terminal until the insertion of a stopCharacter and insert it in a file
*/
fun readInputUntilACharacterAndSave(basePath: String = BASE_PATH,
destinationPath: String, stopCharacter: String = "END"){
try {
val reader = Scanner(System.`in`)
while (true) {
val line = reader.nextLine()
if (line == stopCharacter) break
appendLineToFile(
filePath = basePath + destinationPath,
content = line
)
}
} catch (exception: Exception){
println(exception)
}
}
private fun appendLineToFile(filePath: String, content: String){
try {
val file = File(filePath)
FileOutputStream(file, true).use { fileOutputStream ->
fileOutputStream.write("$content\n".toByteArray())
}
} catch (exception: Exception){
println(exception)
}
}
}