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 interfaces.
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}Streamdoes 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
OutputStreamto 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 aByteArrayOutputStreamrather than aFileOutputStream.Oh, and wrap your
FileOutputStreamin aBufferedOutputStream