Magazine
 
Tips ‘n’ Tricks
 

// Make the thread sleep for one
second.


try{
Thread.sleep(1000);
}
catch (InterruptedException ie)
{
ie.printStackTrace(System.err);
}}
while (!isNewYear);
}
}

Compile and Run:

C:\JavaJazzup>javac
GreetingNewYear.javaC:\JavaJazzup>java
GreetingNewYear

Output:


The output shows the remaining number of seconds. This screen gets updated every second and no of seconds displayed in the screen becomes one less than previous. So this count down goes on till New Year comes.


When New Year comes, it wishes for the
New Year like in the figure below:


5. Save your documents in the directory of your choice in zipped form:

Zip file is a way of packaging a set of files in the compressed form. When a developer needs to use a set of files together, bundling them together in zipped form is one of the most frequently adopted way. When we need to download a group of files from the web,


 

we create a zip file to transport them as a single unit. In this section, we will learn creating zip file containing two files of your choice in the specified directory.

CreateZip.java:

import java.io.*;
import java.util.zip.*;
public class CreateZip{
public static void main(String args[]){
//Collect file names to be zipped in an
array by command line arguments.
String[] filesToZip = new String[]{args[0],
args[1]};
// Create a buffer for reading the files
byte[] buffer = new byte[1024];
try {
// Specify the location of the zipped
file to be stored.
String zip = “c:\\MyDocuments\\
MyZippedDocuments.zip”;
//Creates a new ZIP output stream.
ZipOutputStream out = new
ZipOutputStream(new
FileOutputStream(zip));
// Compress the files for (int i=0;
i<filesToZip.length; i++) {
FileInputStream in = new
FileInputStream(filesToZip[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new
ZipEntry(filesToZip[i]));
//ZipEntry(String name)Creates a
new zip entry with the specified name.
// Transfer bytes from the file to the
ZIP file
int len;
while ((len = in.read(buffer)) > 0)
{

Sept 2007 | Java Jazz Up | 70
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,   Download PDF