How to Copy File?
4 Ways to copy Files
A lots of times in application we need to copy the file from 1 place to another place and its a common task. Here 4 options are illustrated to copy files from 1 location to another.
1. using Stream:
By using this we have to create source file and destination file and after that we have to create input stream for source file and output stream for destination file
Example:
package com.IOFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyFileUsingStream {
public static void main(String[] args) throws IOException {
String SourceFile_Path = "/home/codersarts/file1.txt";
String DestFile_Path = "/home/codersarts/file2.txt";
File sourceFile = new File(SourceFile_Path);
File destFile = new File(DestFile_Path);
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(sourceFile);
os = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}
}
Output
File is Copied
2. Using Files Class
Files class has method copy() to copy the files and its work if you are working on higher version of java > 7.
Example:
package com.IOFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
public class copyFileUsingFileClass {
public static void main(String[] args) throws IOException {
String SourceFile_Path = "/home/codersarts/file1.txt";
String DestFile_Path = "/home/codersarts/file3.txt";
File sourceFile = new File(SourceFile_Path);
File destFile = new File(DestFile_Path);
Files.copy(sourceFile.toPath(), destFile.toPath());
}
}
Output
File is Copied
3. Using java.nio.channels.FileChannel
By using transferFrom() method of FileChannel class we can copy the files and copying the file using this class is more faster than using stream.
Example:
package com.IOFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class CopyFileUsingFileChannel {
public static void main(String[] args) throws IOException {
String SourceFile_Path = "/home/codersarts/file1.txt";
String DestFile_Path = "/home/codersarts/file4.txt";
File sourceFile = new File(SourceFile_Path);
File destFile = new File(DestFile_Path);
FileChannel source_Channel = null;
FileChannel dest_Channel = null;
try {
source_Channel = new FileInputStream(sourceFile).getChannel();
dest_Channel = new FileOutputStream(destFile).getChannel();
dest_Channel.transferFrom(source_Channel, 0, source_Channel.size());
}finally{
source_Channel.close();
dest_Channel.close();
}
}
}
Output
File is Copied
4. Using Apache Common IO FileUtils
if you are using Apache Common IO in your project then you can use the method of this FileUitls.copyFile(File sourceFile, File destFile) to copy the file.
Example:
package com.IOFile;
import java.io.File;
public class CopyFileUsingApcaheCommonUtils {
public static void main(String[] args) {
String SourceFile_Path = "/home/codersarts/file1.txt";
String DestFile_Path = "/home/codersarts/file4.txt";
File sourceFile = new File(SourceFile_Path);
File destFile = new File(DestFile_Path);
FileUtils.copyFile(sourceFile, destFile);
}
}
Output
File is Copied
LineNumberReader Example
This is used to read line number in the files. LineNumberReader has a method getLineNumber() it will return the current line no.
Example:
package com.IOFile;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class LineNumberReaderExample {
public static void main(String[] args) {
String SourceFile_Path = "/home/codersarts/file1.txt";
LineNumberReader lineNumberReader = null;
try {
//Construct the LineNumberReader object
lineNumberReader = new LineNumberReader(new FileReader(SourceFile_Path));
//Print initial line number
System.out.println("Line " + lineNumberReader.getLineNumber());
//Setting initial line number
//lineNumberReader.setLineNumber(5);
//Get current line number
System.out.println("Line " + lineNumberReader.getLineNumber());
//Read all lines and each time increase the line number by 1
String line = null;
while ((line = lineNumberReader.readLine()) != null)
{
System.out.println("Line " + lineNumberReader.getLineNumber() + ": " + line);
}
}
catch (Exception ex) {
ex.printStackTrace();
} finally {
//Close the LineNumberReader
try {
if (lineNumberReader != null){
lineNumberReader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Output
Line 0
Line 0
Line 1: This is tutoiral for java beginners
Line 2: Hello CodersArts