Magazine
 

Tips & Tricks

 

1. Copy content from one file to another This example explains how to copy contents from one file to another file. Copy file is one of the good use of io package of Java. The logic of program is explained below:

Explanation
This program copies one file to another file. We will be declaring a function called copyfile which copies the contents from one specified file to another specified file. copyfile(String srFile, String dtFile) The function copyfile(String srFile, String dtFile) takes both file name as parameter. The function creates a new File instance for the file name passed as parameter

File f1 = new File(srFile);
File f2 = new File(dtFile);

and creates another InputStream instance for the input object and OutputStream instance for the output object passed as parameter

InputStream in = new FileInputStream(f1);
OutputStream out = new
FileOutputStream(f2);

and then create a byte type buffer for buffering the contents of one file and write to another specified file from the first one specified file.

byte[] buf = new byte[1024];
out.write(buf, 0, len);

CopyFile.java:

import java.io.*;
public class CopyFile{
private static void copyfile(String srFile,
String dtFile){
try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new
FileInputStream(f1);
OutputStream out = new
FileOutputStream(f2);

 

byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println(“File copied.”);
}
catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + “
in the specified directory.”);
System.exit(0);
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args){
switch(args.length){
case 0: System.out.println(“File has not
mentioned.”);
System.exit(0);
case 1: System.out.println(“Destination
file has not mentioned.”);
System.exit(0);
case 2: copyfile(args[0],args[1]);
System.exit(0);
default : System.out.println(“Multiple
files are not allow.”);
System.exit(0);
}
}
}

Output

C:\javajazzup>javac CopyFile.java
C:\ javajazzup>java CopyFile a.java
Filterfile.txt
File copied.
C:\ javajazzup>

2. Pop-up Menus

A PopupMenu is similar to a Menu as it contains MenuItem objects. The Pop-up Menu can be popped over any component while generating the appropriate mouse event rather than letting it appear at the top of a Frame. Menu class can only be added to a Frame and not to the Applet.

Jan 2007 | Java Jazz Up | 96
previous
index
next
 
View All Topics
All Pages of this Issue
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 , 80 , 81 , 82 ,

83, 84 , 85 , 86, 87 , 88, 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 , 101 , 102 , 103, 104 , 105 ,

106, 107,

Download PDF