I attempted to create a sort-of file generator which spits out hardcoded messages to a file with slight alterations to some of the strings based on user input. Since I'm utilizaing polymorphism, I have multiple files and inte开发者_JAVA技巧rfaces.
I'm finding myself passing a file parameters more times than one and is starting to rethink the way I've structured my program. Which brings me to ask, is there a huge performance impact from passing a file as a parameter to multiple methods?
Thanks.
There's no measurable performance impact from the number of parameters that you pass to a method.
However, repeatedly opening and closing a java.io.File{Input|Output}Stream
does have a cost. And you need boilerplate try/finally code to ensure that the file is properly closed after use.
A better solution is to pass an OutputStream
to your methods, and open the file once at the top-level method. This will also allow your code to be most easily tested: you can pass a ByteArrayOutputStream
rather than a FileOutputStream
.
Oh, and wrap your FileOutputStream
in a BufferedOutputStream
I'm not quite sure what you're really passing around, and some code examples would be useful.
My first thought is that if you have performance issues, then you should be profiling your application. Writing a file will likely be the bottleneck, rather than argument passing (remember - you'll be passing object references, not the objects themselves).
But measure your application performance first, before you embark on any (possibly costly) refactorings.
精彩评论