You could have moved that system messages also into methods with which they are associated and code would be little more readable :)
peter_budo
Code tags enforcer
15,788 posts since Dec 2004
Reputation Points: 2,867
Solved Threads: 944
Skill Endorsements: 50
Few points:
- close() calls should always go in the "finally" blocks
- Never use System.exit() to duck out in case of failure. If your code is used in a large/big setting, exitting the JVM in case something fails isn't pleasant. Boolean returns are your friend.
- Always check the return status of "delete()" call because file deletion failures are pretty common.
~s.o.s~
Failure as a human
12,220 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 783
Skill Endorsements: 55
it just increases the size of your code because then you are importing everything from the IO package
This statement is a bit misleading. It doesn't increase the size of your code. If you'll disassemble the class file, you'll notice that classes are anyways referenced using their fully qualified names (i.e. java.io.BufferedReader instead of simply BufferedReader). The imports are anyways used to manage/locate compile time dependencies.
That being said, the reason wildcard imports are not recommended (unless throwing together snippets) is that it becomes difficult to trace which class was imported from what package. This of course if mitigated by using an IDE but is a good thing to know just in case.
~s.o.s~
Failure as a human
12,220 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 783
Skill Endorsements: 55
a semi nOOb? if you work hard enough, you can be an expert nOOb in no time ;)
something you could also have changed in your code, to make it a bit more reusable:
public static ArrayList<String> readFile(String fileName) throws IOException{
ArrayList<String> fileContents = new ArrayList<String>();
... // read file and store lines in the ArrayList
return fileContents;
}
and a bit the same for writing the file.
stultuske
Industrious Poster
4,370 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
main advantage is: when you start reading, how are you supposed to know how many lines are in the file? the ArrayList will automatically increase it's size when your predefined (or default) sized list is full (probably not the best way to explain it, but hey :) ) while you can't enter a sixth element in an array which is instantiated to have only five elements.
stultuske
Industrious Poster
4,370 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
Hmm yes in that way i can see it as very useful, but at the same time you could just have a integer that's not declared to anything and then once the size is known initiate the integer with appropriate size. The trick is to only initialized the array once you have initialized the integer ;). But yes you are right its much easier... but hey i cant go down without a few words hehehe
down side would be that you have to read the file twice ... once to find out how many lines of text are in your file, and once after you've initialized the array to fill it with the contents :)
stultuske
Industrious Poster
4,370 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24