Posts

Showing posts with the label IO

Strings , I/O and formatting (Part-5 File I/O)

Image
Let's start coding on what we have learnt in the previous post. File f=new File("me.txt"); System.out.println(f.exists()); Here we created a File object and checked for the presence of the file using f.exists() method.If you don't have a me.txt inside the project folder this gives false as the output and if you do have  true as the output.Let's move on and create a new file. boolean a = f.createNewFile(); System.out.println(a+" "+f.exists()); Here we created a new file and check whether the process is successfully finished and the file is created.Note that createNewFile() creates a new file if it doesn't already exists.If the file exists you get false as the output from the above method. One thing to remember is that you have to put the file operation codes inside a try catch block as the operations declare checked exceptions.Alright...!!now we'll start writing text to files. File f=new File("me.txt"); FileWriter...

Strings , I/O and formatting (Part-4 StringBuffer and StringBuilder,File I/O)

Image
In the previous post we have discussed about some basic facts about StringBuffer and StringBuilder classes.At the end we have instantiated a StringBuffer object,applied some changes and logged the output and saw the differences between String and StringBuffer classes. Same rules apply to the StringBuilder class. StringBuilder a=new StringBuilder("hasitha"); a.append(" is a good boy"); System.out.println(a); Prints out " Hasitha is a good boy ".Now you are completely aware about how StringBuffer and StringBuilder classes behave when handling strings.Let's study some important methods in these classes.You have already have an idea about the append method.Note that append is a synchronized method for StringBuffer class.As in the previous post I am going to provide you the link for all the methods in StringBuffer and StringBuilder class.(Self study is the best....!!!!) StringBuffer https://docs.oracle.com/javase/7/docs/api/java/lang/StringB...

Strings, I/O and formatting (Part-1)

Image
"Hey Isla Are you serious??Strings,IO and formatting???"Hmmm..yeah I agree with you.This is basic Java.Anyway no problem we'll go further and discuss and see how much you know about Java Strings,I/O and formatting.We'll start with a little background information about strings. Strings are immutable objects.(You know what I mean) String is a 16-bit Unicode character In java string is an object.You can create a new instance using new keyword String myString=new String(); This creates a new string object and assign it to myString reference variable.Cool..Now we need to assign a value to the new variable.Let's do it like this. myString="Hasitha"; And we are done.Now we have a string variable with a value.Is this the only way we can do this.Absolutely no.String class has a number of constructors.We can do the same thing as follows. String myString=new String("Hasitha"); String myString="Hasitha"; Hmmm...Note that there...