I am now trying to create a new file for eg: (d:\test\testFile.txt).
If there is no file in such directory how can i create then. If i tend to open the file again then the existing contents of the file needs to be removed and the new content should be the content later.
I made use of the File file = new File("d:\test\testFile.txt"); file.exists()
BufferedWriter out = new BufferedWriter(new FileWriter("d:\test\testFile.txt"));
How could i get make the file created, if the file does not exists and fill up the new contents to the file erasing up the previous contents?
Before writing on the file (using BufferedWriter as you have shown in your post), you have to do the following:
Check if the file exists. (Using file.exists() method)
If it exists, delete the file (Using file.delete() method)
After this you can keep writing to the file. Make sure you call flush() on the writer after you are done (Close() internally will call flush(), so if you do not need the writer you can close it). This will force the contents on to the file system.
P.S.: You do not need to create a new file before writing on it. A write will implicitly create the file if it is not present.