Friday, June 12, 2009

Copy, move and delete files using Java

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.

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

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


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)
            if (out != null)
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 \
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/
In same configuration file add PHP handler-
<Directory "/apache_install_dir/htdocs">
     . . . .
     . . . .
     <FilesMatch \.php$ >
          SetHandler application/x-httpd-php
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
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.