I am reading a log file through buffered reader mechanism which is taking Total execution time taken in millis: 12944 ,please advise how can I improve the performance and bring down this time , Please advise is nio is more better performance than buffered Reader..!! The file size is of 10MB since it is a log file..!! please advise also how this same thing could be achieved with nio also..!!
public class BufferedRedeem
{
public static void main(String[] args)
{
BufferedReader br = null;
long startTime = System.currentTimeMillis();
try
{
String sCurrentLine;
br = new BufferedReader(new FileReader("C://abc.log"));
while ((sCurrentLine = br.readLine()) != null)
{
}
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println("Total execution time taken in millis: " + elapsedTime);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)
br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
Since the OP is keen to see how this could be done using NIO.
As the file is small, it is hard to see the difference but it can be measured.
prints each line is 102 bytes long so the file is ~ 10 MB.
As I mentioned before, its unlikely to be worth the extra complexity of using NIO to save 35 ms.
BTW: If you have a HDD and the file is not in memory, only the speed of your drive will matter.