Sunday, April 26, 2009

Famous thieves !!

Hi everyone,
I was listening to BBC Extra on BBC Arabic radio - It's a very nice show - any way they were talking about copyrights and how much they are protected in the Arab world.
They talked about a site that some one made, this site tries to show you very famous writers who actually stole some wrtings from the Internet or from else where and published these writings under their name. There are also posters of actors and singers. Even cartoon drawn has been stolen by some artists.

The matchs are so identical that no one can say that it is a coincidence.

If you are interested visit the site and you will be amazed !!

What is this part of the blog?

Hi everyone,
In this part I will be talking about any thing interesting that I heard about. ofcourse your comments are welcomed.

Saturday, April 25, 2009

Writing and Appending

Hi everyone,
When you are programming, you will need to store data on files. some times you want to discard all the contents of the file and write all the data right from the beginning, and other times you want to add just a line to the data in file.

The first type is called writing while the second one is named appending. The default when you create a FileWriter in JAVA it will overwrite all the data in the file.


FileWriter writer = new FileWriter("sample.txt");
writer.write("Trying writing in file");
writer.close();


If you run this code, the FileWriter "writer" will delete all lines in "sample.txt" and write from the beginning of the file. If the file doesn't exist it will be created and the "writer" will write in it.
Note:
1 - You must always close any input or output streams in order to perform appropriate operations.
2 - You will have to import java packages:

import java.io.FileWriter;
import java.io.IOException;

3 - You will have to throw or try and catch IOException.




Let's see the appending.
Try this code


FileWriter writer = new FileWriter("sample.txt");
writer.write("The first line was deleted");
writer.close();


You will find only one sentence "The first line was deleted", the other sentence we wrote on the first coding doesn't exist any more. Sometimes there is no problem with that, but what if you want to add the new line with out deleting the already written one ?

To answer the question try this thing:


FileWriter writer = new FileWriter("sample.txt", true);
writer.write("\nThe first line was not deleted");
writer.close();


The true that we added to the constructor means that we want the FileWriter to append the new data to the existing data.

These Images will show every thing


Writing in the file:




Overwriting the data in the file:




Appending to the file:








Well that's all for now, See soon isa.

What is this part of the blog?

Hi everyone,
In this part of the blog I will try to provide you with some tips on coding on Java. I am no expert but I will be publishing small snippets that are easy but maybe you don't know still they can be very helpful and usefull.
Thanks,
Khaled Abd Al Hady @ CSD