
Download PDF

Download E-Books
JAVA GEEKS ZONE

[Source: bigdownload.blogspot.com]
Say x & y are the integer variables. The challenge is to swap them without using a temporary variable.
The solution is as simple as the problem itself:
x = x + y;
y = x - y;
x = x - y;
Update 1: Jack and Alexey pointed out a typo in my solution which has since been corrected. Please see their comments below.
The simplicity of the solution appeals to me. It clearly demonstrates the meaning of assignment operator ( "=" ).
What if they are Strings?
Note: You can use String methods in Java API.
Here is the solution:
x = x + y;
y = x.substring(0, x.indexOf(y));
x = x.substring(x.indexOf(y) + y.length());
Can you see the similarity?
Can you provide a simpler solution to either of the above?
Update 2: Robert just did (see his comment below). He pointed out that the String solution doesn't work when one string is contained in another. His solution is:
x = x + y;
y = x.substring(0, x.length() - y.length());
x = x.substring(y.length());
Replace *.html with file name(s) and directories you want to update. This requires JDK 1.5 or later.
import java.io.File;
/** Super-fast file / directory(recursive) touch.
* It doesn't ask for confirmation.
* Arguments: File / Directories to touch to current time.
*/
public class Touch {
public static void main(String ... args) {
long time = System.currentTimeMillis();
for(String fileName:args) touch(new File(fileName), time);
}
/** Recursively touch file and directories.
* @param File (file or directory) for touching.
*/
public static void touch(File file, long time) {
if(file.isDirectory()) for(File childFile:file.listFiles()) touch(childFile, time);
file.setLastModified(time);
}
}
/** Super-fast file / directory(recursive) touch.
* It doesn't ask for confirmation.
* Arguments: File / Directories to touch to current time.
*/
public class Touch {
public static void main(String ... args) {
long time = System.currentTimeMillis();
for(String fileName:args) touch(new File(fileName), time);
}
/** Recursively touch file and directories.
* @param File (file or directory) for touching.
*/
public static void touch(File file, long time) {
if(file.isDirectory()) for(File childFile:file.listFiles()) touch(childFile, time);
file.setLastModified(time);
}
}
/** Super-fast file / directory(recursive) touch.
* It doesn't ask for confirmation.
* Arguments: File / Directories to touch to current time.
*/
public class Touch {
public static void main(String ... args) {
long time = System.currentTimeMillis();
for(String fileName:args) touch(new File(fileName), time);
}
/** Recursively touch file and directories.
* @param File (file or directory) for touching.
*/
public static void touch(File file, long time) {
if(file.isDirectory()) for(File childFile:file.listFiles()) touch(childFile, time);
file.setLastModified(time);
}
}
/** Super-fast file / directory(recursive) touch.
* It doesn't ask for confirmation.
* Arguments: File / Directories to touch to current time.
*/
public class Touch {
public static void main(String ... args) {
long time = System.currentTimeMillis();
for(String fileName:args) touch(new File(fileName), time);
}
/** Recursively touch file and directories.
* @param File (file or directory) for touching.
*/
public static void touch(File file, long time) {
if(file.isDirectory()) for(File childFile:file.listFiles()) touch(childFile, time);
file.setLastModified(time);
}
}
String[] poList = pos.split("\r\n|\r|\n");
What did I do?
I am splitting the String based on a regular expression which looks for carriage return (\r) or line feed (\n) or carriage return immediately followed by line feed. These takes care of all types of newlines you may encounter. It returns, as you can see, a String array of results. Trailing empty strings are not included in the resulting array.
What's interesting?
The following will include a whole bunch of empty Strings in the result.String[] poList = pos.split("\r|\n|\r\n");
Can you tell why?
How many alternatives can you tell like using StringTokenizer for example?
This old but still golden article is an excellent guide to using Runtime.exec(). The key points he discusses are:
1. You need to drain the input stream to prevent because failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
2. Use waitFor() instead of exitValue() when you want to wait for the process to finish.
3. Runtime.exec() wouldn't directly execute shell commands like dir / ls, copy / cp etc. You need to invoke the shell cmd.exe / bash / sh and pass the shell commands. For example in windows your command array to execute dir would be as follows:
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = "dir";
Personally I have successfully used Runtime.exec() on several occasions. Any C programmer should quickly find equivalence with fork and system calls in C language. As always RTFM.

The files are added to a queue first and then uploaded. The live file upload status and statistics are shown. The extension is pretty stable and usable. You can see a demo here. It is based on high quality Ext framework and hence is cross browser compatible.
The server side of the code is not provided. So it is for you to explore and find. A sample server side implementation say in PHP or Java is what is missing from this otherwise excellent piece of work.
Details here.