Java: Outputting text file to Console -


i'm attempting output text file console java. wondering efficient way of doing so?

i've researched several methods however, it's difficult discern least performance impacted solution.

outputting text file console involve reading in each line in file, writing console.

is better use:

  1. buffered reader filereader, reading in lines , doing bunch of system.out.println calls?

    bufferedreader in = new bufferedreader(new filereader("c:\\logs\\"));  while (in.readline() != null) {       system.out.println(blah blah blah);           }          in.close(); 
  2. scanner reading each line in file , doing system.print calls?

    while (scanner.hasnextline()) {      system.out.println(blah blah blah);    } 

thanks.

if you're not interested in character based data text file containing, stream "raw" bytes.

inputstream input = new bufferedinputstream(new fileinputstream("c:/logs.txt")); byte[] buffer = new byte[8192];  try {     (int length = 0; (length = input.read(buffer)) != -1;) {         system.out.write(buffer, 0, length);     } } {     input.close(); } 

this saves cost of unnecessarily massaging between bytes , characters , scanning , splitting on newlines , appending them once again.

as performance, may find this article interesting. according article, filechannel 256k byte array read through wrapped bytebuffer , written directly byte array fastest way.

fileinputstream input = new fileinputstream("c:/logs.txt"); filechannel channel = input.getchannel(); byte[] buffer = new byte[256 * 1024]; bytebuffer bytebuffer = bytebuffer.wrap(buffer);  try {     (int length = 0; (length = channel.read(bytebuffer)) != -1;) {         system.out.write(buffer, 0, length);         bytebuffer.clear();     } } {     input.close(); } 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -