Fast file copy and move by java

January 11, 2009

The following method does very fast file copy from one location to another location.

public boolean copyTo(File source, File destination) {
try {
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(destination);

byte []buf = new byte[4096];
int loaded = 0;
while ((loaded = fis.read(buf)) > 0 ) {
fos.write(buf, 0, loaded);
}
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}

Here i used 4096 byte buffer thats the key for fast copy.this value is optimize value from from different tested values.

The following method does very fast file move from one location to another location.

public boolean moveTo(File source, File destination) {
boolean isRenamed;
if(isPathExists(destination.getAbsolutePath())){
destination.delete();
}

isRenamed = source.renameTo(destination);
if(!isRenamed){
copyTo(source,destination);
source.delete();
}
return isRenamed;

}

public boolean isPathExists(String path){
File location = new File(path);
return location.exists();
}

The key of fast moving is the rename function.


Filter data when insert into List

January 4, 2009

We use list for storing data but there is no way to insertation filter. I mean when i want to insert a specific type of data like a list that always store which string has this value. here i gave one example list that  always filterine during the insertation period. lets consider we have one collection that contain all the car make names. now we need one list that will store only Honda make name.

I used here two class HondaList and ToyotaList that act as list.

———————-HondaList.java

import java.util.LinkedList;
class HondaList extends LinkedList<String> {

public HondaList() {
super();
}

public boolean add(String make) {
if (make.indexOf(“Honda”) == -1) {
return false;
} else {
super.add(make);
return true;
}
}
}
—————- ToyotaList.java

import java.util.LinkedList;
class ToyotaList extends LinkedList<String> {

public ToyotaList() {
super();
}

public boolean add(String make) {
if (make.indexOf(“Toyota”) == -1) {
return false;
} else {
super.add(make);
return true;
}
}
}

—————— Main Class

public class ListDataFilterDemo {
private HondaList hondaList;
private ToyotaList toyotaList;

public ListDataFilterDemo() {
this.hondaList = new HondaList();
this.toyotaList = new ToyotaList();
}

public void addData(){
hondaList.add(“Honda Civic 2002″);
hondaList.add(“Honda Accord 2002″);
hondaList.add(“Toyota Corolla 2002″);

toyotaList.add(“Honda Civic 2002″);
toyotaList.add(“Honda Accord 2002″);
toyotaList.add(“Toyota Corolla 2002″);

for (String make : hondaList) {
System.out.println(make);
}

for (String make : toyotaList) {
System.out.println(make);
}
}

public static void main(String[] args) {
try {
ListDataFilterDemo demo = new ListDataFilterDemo();
demo.addData();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

————- Output

Honda Civic 2002
Honda Accord 2002
Toyota Corolla 2002

HondaList adds all data except Honda data same ToyotaList does.


Follow

Get every new post delivered to your Inbox.