I am reading XML files from a directory (containing thousands of files), processi开发者_如何转开发ng the content and writing one output file for each input file. Is there a way of 're-pointing' an existing BufferedWriter
instead of creating a new instance for each file?
...
Scanner scanner;
BufferedWriter writer;
File outfile;
for (File f: directory.ListFiles[]){
scanner = new Scanner(f);
outfile = ...;
// processing input
writer = new BufferedWriter(new FileWriter(new File(outfile)));
// write the content
writer.flush();
writer.close();
}
...
It seems a waste to have to create thousands of iterations of the Scanner
and BufferedWriter
.
To me this would sound like a premature optimization. JVM is one of the smartest pieces of the software on the planet and can detect if your objects are short-lived. It can (and will) also perform a boatload of optimizations. Most likely any micro-optimizations you attempt to make will only result in poor performance.
Key to performance optimization is to measure, not to reason.
精彩评论