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,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
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
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Thank you all for the input... to those using the code, now you know how to perfect it :).
i would do it but i cant edit the snippet.
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
or you could just import :
import java.io.*;
it just increases the size of your code because then you are importing everything from the IO package.
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
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
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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 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.
Hmmm okay i see maybe i was wrong :P but i find the scanner method so much quicker and simpler to read files... but thank you now i do know that.... maybe my misconception came from back in the day when i was a REAL nOOb now im a semi nOOb lol
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
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
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
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.
Lol :)... and yes i could have used an array list but the main reason was it would have changed the whole method i used to find and replace certain text. maybe not by much but it still would have been very different then my string buffer method. And also i cannot see any advantage of an array list over a normal array(in this circumstance) other then some of the capabilities like remove() but it can also be done in an array.. but has to be in a for statement to find the data to remove... but maybe thats because i havent taken the time to get to know it;)
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
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
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
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.
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
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
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
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
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 :)
Lol smart....:D
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169