0

How to make this code faster (reading text file then splitting it part by part at a charater and then output it --in java--

https://code.sololearn.com/cmv18GLGrl6V/?ref=app

11th Dec 2018, 5:17 PM
Khaled
Khaled - avatar
7 Antworten
+ 2
Call str.split outside the loop, store the result and use it anytime that you need it. Actually you call it 3 times at iteration, while one time (totally) is enough. Anyway, you can: - Read the input file size - Create a preallocated buffer string of file size - Copy input file content in this buffer - Process this buffer like you doing
11th Dec 2018, 6:31 PM
KrOW
KrOW - avatar
+ 1
I searched on Internet, I didnt find an answer to me (my english is not so good), so I tried my code above
11th Dec 2018, 5:26 PM
Khaled
Khaled - avatar
0
I'm pretty sure bufferreader are more faster than scanner. But considering the file size sometimes scanner is more than enough, i mean probably there's only few ms difference in small files.
11th Dec 2018, 5:23 PM
Taste
Taste - avatar
0
the file is just 6Mb, and the process is so slow, when I try with a 100Kb file, everything is perfect
11th Dec 2018, 5:25 PM
Khaled
Khaled - avatar
0
Well maybe you cam try buffer reader And bit of suggestion, put file writer outside the loop because it'll keep creating a new one each itteration. Instead save all of the data in string, then write them to file after the code finish reading.
11th Dec 2018, 5:29 PM
Taste
Taste - avatar
0
That makes me spin, how to put file writer outside the loop, there will be many files that should be created so how to create them?!
11th Dec 2018, 5:36 PM
Khaled
Khaled - avatar
0
I mean the writer instance not the file, dont worry. Instead of doing that create a StringBuffer before reading the file. Then just append() the text you want to write later on to it. Once the reading done, you can start initialize your file writer then write the content in StringBuffer to a file
11th Dec 2018, 5:46 PM
Taste
Taste - avatar