Tuesday, 10 May 2011

Copy multiple files (java)

We have already seen the function to copy 1 file to another destination. Here we have to write function to copy multiple file to 1 location.

Refer this article for copyFile() function.

public static boolean copyMultipleFiles(String dst, String ... src){
for(String oneSrc : src)
{
boolean isCopied = copyFile(oneSrc,dst);
if(!isCopied)
return isCopied;
//Note that i have not yet figured out the case of rollback in case of
//unsuccessful copy of 1 file. Because this depends on your application.
//So if you want to rule out any copy if even 1 copy file fails,
//just call the delete function and so on.
}
return true;
}



Also note that this method uses varargs function, but you can use array instead.

Java - Copying one file to another

Here is function called copyFile  which takes 2 strings – source and destination. It copies source to destination.

 

private static void copyFile(String srcFile, String dstFile){

try{

File f1 = new File(srcFile);

File f2 = new File(dstFile);

InputStream in = new FileInputStream(f1);

//Append the file
OutputStream out = new FileOutputStream(f2,true);
//Overwrite the file.
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."
);
return false;

}

catch(IOException e){

System.out.println(e.getMessage());
return false;

}
return true;
}

Rename the File or Directory in java

This example shows function, which renames the file or directory. The function takes old name and new name, and renames accordingly.

public boolean renameFile(String oldName, String newName)
{
File oldfile = new File(oldName);
if(!oldfile.exists())
{
System.out.println("File or directory does not exist.");
return false;
}
File newfile = new File(newName);
boolean reName = oldfile.renameTo(newfile);
if(!reName )
{
System.out.println("File or directory does not rename successfully.");
return reName ;
}else{
System.out.println("File or directory rename is successfully.");
return reName;
}
}

Create a File

For this createNewFile() method is used. It returns true if file is created and false otherwise. So for eg. it returns false if file already exist, so it can't create the new file here.

import java.io.*;
//now wherever you want do this:
public static void main(String[] args){
File file = new File("C://demo.txt");
boolean isCreated;
try
{
isCreated= file.createNewFile();
}
catch(IOException ioe)
{
System.out.println("Error in creating a new empty file :" + ioe);
}
}

Monday, 9 May 2011

Working with Directory tutorial

Following topics can be covered in this tutorial

Standard Streams in java

Standard Streams are a feature provided by many operating systems. By default, they read input from the keyboard and write output to the display. They also support I/O operations on files.

Java also supports three Standard Streams:

  • Standard Input: Accessed through System.in which is used to read input from the keyboard.
  • Standard Output: Accessed through System.out which is used to write output to be display.
  • Standard Error: Accessed through System.err which is used to write error output to be display.

These objects are defined automatically and do not need to be opened explicitly. Standard Output and Standard Error, both are to write output; having error output separately so that the user may read error messages efficiently.
System.in is a byte stream that has no character stream features. To use Standard Input as a character stream, wrap System.in within the InputStreamReader as an argument.

InputStreamReader inp = new InputStreamReader(system.in);

Using DataInputStream and DataOutputStream