Showing posts with label File Read and Write. Show all posts
Showing posts with label File Read and Write. Show all posts

Wednesday, June 24, 2015

PDF File Generation example in Java using iText Library

made an example where reports can be generated in PDF file format using iText library for Java. I made a swing app and we enter the data and then click the create pdf button to generate the pdf. Its not a tutorial but more like an example with a demo. Use the code, and I hope its readable enough.

[Image: 2Ssa87i.png]


Continue Reading →

Monday, June 15, 2015

Read Character Files line by line in Java version 6 or less

Duke, the Java Mascot, in the waving pose. Duk...
Duke, the Java Mascot, in the waving pose. Duke images are now Free Graphics see duke:Project Home Page for more details. (Photo credit: Wikipedia)

In this post I am sharing a code on how to read character files line by line for Java version 6 or less. The current java version being 8 and the way to read the files is a bit different, like we use Java NIO primarily. Still for the sake of knowledge this post will help you clear some concepts.

The following source shows you the way to read a file in Java version 6 or less (compatible with JDK 5 but not 4)

It covers most of the error handling cases possible and note that we should close BufferedReader when not in use. The following prints the time taken by the code to read the file and total number of the contents read (In this case I read a file containing all the words of a dictionary)



Related articles
Continue Reading →

Sunday, June 14, 2015

How To copy a Very large file to a flash drive in Linux

 

Say that you have a very large file say 6GB and you want to copy the entire file into a flash drive with FAT32 file system where the maximum size for each file can be around 4 GBS ((2^32) - 1 bytes). If you want to copy them then you have got two choice :-

1. Format the USB with some other file system which will allow files of size greater than 4 gb, like NTFS.
2. Split the Large file into smaller sizes and write to USB and then concatenate them into a single file.

I prefer the second method and so we will learn how to do this.

Step 1

Split the file into smaller sizes and write it to flash drive. (Make sure you have mounted your flash drive already)

split --bytes=2G VeryLargeFIle.txt /media/38B9-D21F/VeryLargeFile.txt. 

In th above command we are splitting a text file (it could be any file) into smaller files of size 2GB and storing in the location where our flash drive was mounted.

Now if look into your mounted drive folder you will see smaller files with sequential extensions were created like VeryLargeFile.txt.aa, VeryLargeFile.txt.ab ......

Step 2

Now we concatenate the smaller files into a single file. We can do this using the cat command. Simply copy the three smaller files from usb to another location in a different pc (Do whatever you want). Move to that location using terminal and then execute the following command. 

cat VeryLargeFile.txt.aa VeryLargeFile.txt.ab VeryLargeFile.txt.ac > VeryLargeFile.txt


Taddah! You now have your very large file moved to another system and in another location in one piece. You can remove the smaller sized files which were created earlier in the USB.

I hope you like this little piece of advice or trick. This technique will work on any file with any format.

Share this post among your social networks.

Thank you.
Sincerely,
Psycho_Coder
Continue Reading →

Saturday, June 13, 2015

Java Code to read Character File using FileReader.

I this post I have given an code that demonstrates how to read a character file using FileReader and try-with-resources in Java. Just like the post title says its not a tutorial but rather an example demonstration.

Try-With-Resources is a great feature introduced in Java 7 where you don't need top worry about explicitly closing the streams. It cares of them, It does so by implementing the AutoClosable iterface.

Please Note that this is not the better way to read a file. There are several other ways using the NIO and NIO2 packages which should be preferred for larger file sizes. All of them will be covered with time.


Share this post among your social networks. 

Thank you.
Continue Reading →

Write a File using Java NIO

The following example shows how to write a file using Java NIO. It creates a file named loginlist.txt, if the file already exists then it will remove it. The following code also generate random strings of arbitrary length. The strings are written in the following format :-

<Random Strings> : <Random Strings>

It writes 10 million entries and takes 32 seconds in my PC (Intel Core i3 2nd Gen, RAM - 4 GB) to complete and file size reaches to about 180 MB.

Continue Reading →

Follow Me!

Followers

Visitor Map