Friday, June 12, 2009

Copy, move and delete files using Java

Delete
Deleting files in Java is fairly simple, just one method call-

   new File("file path").delete();

Only caveat here is that 'non-empty' directories can not be deleted. Of course you will need to have write permission (required for all write operations on file) on that directory as well.

Move
Moving files is also as simple as deleting, just one method call-

   new File("source file path").renameTo(new File("destination file path"));

Copy

Copying files in Java needs some heavy lifting as there is no API to do this task. Here is an example to copy files from one directory to another-

public void copyFiles(String source, String destination) throws IOException {
    File srcDir = new File(source);
    File[] files = srcDir.listFiles();

    FileChannel in = null;
    FileChannel out = null;
    for (File file : files) {
        try {
            in = new FileInputStream(file).getChannel();
            File outFile = new File(destination, file.getName());
            out = new FileOutputStream(outFile).getChannel();
            in.transferTo(0, in.size(), out);
        } finally {
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        }
    }
}
The above example uses NIO API from Java 5, which finishes task pretty fast.

Saturday, June 06, 2009

Spell checking with FCKeditor

FCKeditor is an excellent tool for providing rich text editing experience on web. Best part is that it works almost out of the box, there is very little to configure. Spelling checking is an exception here. Though spell checking also works out of the box, but with Internet Explorer only, which is not the only browser out there. Recently I used FCKeditor in a Java web application and had to integrate the spell check functionality as well. The spellar pages + aspell + PHP combination works pretty well for this task. FCKeditor comes with perl, PHP and Cold Fusion scripts for spell checking, I used PHP. In this post I will try to outline the steps required to make spell check working in FCKeditor.

1. Install PHP
PHP installation (I did it on Fedora) is pretty simple, just download the source and execute following command sequence-

tar –xvzf php-5.2.9.tar.gz
cd php-5.2.9
./configure --prefix=/path_to_install \
         --with-apxs2=/apache_install_dir/bin/apxs \
         --disable-cli \
         --disable-cgi
make
make install
For more details about PHP installation see PHP documentation.

2. Integrate PHP with Apache
Apache needs to be instructed to invoke PHP interpreter for all requests for PHP files ('.php' extension). First we need to include PHP module by adding following line in /apache_install_dir/conf/httpd.conf-
LoadModule php5_module modules/libphp5.so
In same configuration file add PHP handler-
<Directory "/apache_install_dir/htdocs">
     . . . .
     . . . .
     <FilesMatch \.php$ >
          SetHandler application/x-httpd-php
     </FilesMatch>
</Directory>
3. Install aspell and dictionaries
Aspell provides dictionaries for all major world languages. Installation of aspell and its dictionaries is fairly straightforward. After downloading aspell and dictionaries execute following command sequence for each of the downloaded archive-
tar –xvzf archive_name.tar.gz
cd archive_name
./configure
make
make install
4. FCKeditor Configuration
By default FCKeditor is configured to use 'ieSpell', change it to use 'SpellarPages' instead. Also make sure it uses PHP and does spell checking in FireFox. Following properties needs to be changed in 'fckconfig.js'-
FCKConfig.SpellChecker = 'SpellerPages' ;
FCKConfig.SpellerPagesServerScript = 'server-scripts/spellchecker.php' ;
FCKConfig.FirefoxSpellChecker = true ;
Now configure the language and 'aspell' path in 'spellchecker.php'-
$aspell_prog = 'aspell';
$lang = 'en_US';

Now we are ready to test the application and see spell check in action. All software versions being used for this post are outlined below-
  • Apache 2.2.11
  • PHP 5.2.9
  • FCKeditor 2.6.4 (with bundled Spellar Pages)
  • Aspell 0.60.6
  • Tomcat 6.0.18
  • Java 6 update 13
  • Fedora 9
To know more about using Apache to serve Java application see Apache in from of Tomcat and JBoss.

Sunday, May 17, 2009

Install Linux (Ubuntu) from USB drive

Recently one of our old laptop was crashed, so I had to re-install the operating system. We lost the Windows XP CD came with it, so installed XP from another CD. New installation was not ready to accept the keys mentioned on the sticker at the bottom of the laptop. So I decided to install Ubuntu 9.04 using a USB drive. This document has detailed information about USB installation. As no Ubuntu installation was available at that time, so creating a boot USB drive from downloaded ISO file was the only option and UNetbootin did the task nicely on Dell Latitude D630. But IBM ThinkPad R51 refused to boot from the USB disk (Kingston DataTraveler 4GB), anyway I was prepared for such failures and tried with another USB drive (Sandisk Cruzer micro 2GB), which worked well. On first boot system recognized my wireless network and connected instantly along with nice sound blowing from speakers.

My experience with USB installation was pretty nice and it took just 15 minutes for the whole exercise. Ubuntu installation was much easier then Windows XP.