Posts

Showing posts with the label Java Io

Create A Directory If It Does Not Exist And Then Create The Files In That Directory As Well

Answer : This code checks for the existence of the directory first and creates it if not, and creates the file afterwards. Please note that I couldn't verify some of your method calls as I don't have your complete code, so I'm assuming the calls to things like getTimeStamp() and getClassName() will work. You should also do something with the possible IOException that can be thrown when using any of the java.io.* classes - either your function that writes the files should throw this exception (and it be handled elsewhere), or you should do it in the method directly. Also, I assumed that id is of type String - I don't know as your code doesn't explicitly define it. If it is something else like an int , you should probably cast it to a String before using it in the fileName as I have done here. Also, I replaced your append calls with concat or + as I saw appropriate. public void writeFile(String value){ String PATH = "/remote/dir/server/"; ...

BufferedReader Vs Console Vs Scanner

Answer : BufferedReader Since Java 1.1 Throws checked exceptions Can read chars, char arrays, and lines Fast Scanner Since Java 1.5 Doesn't throw checked exceptions Can read lines, whitespace-delimited tokens, regex-delimited tokens, and numbers Difficult to read single characters Console Since Java 1.6 Doesn't throw checked exceptions Can read lines Underlying reader can read chars and char arrays (stops at line bounds) Not always available (e.g. Eclipse) Can read passwords (i.e. read without displaying the characters) Recommendation: Scanner The methods for reading numbers are very useful. The exceptions are unchecked, so you do not have to write boilerplate try/catch blocks. Console class is implemented in a platform independent way to handle the console input for different Os. All OS has a console/shell but they are quite different in implementation. So Console class gives you a Java platform independent runtime class to access things like...