init
This commit is contained in:
119
src/net/droidtech/io/DroidFile.java
Normal file
119
src/net/droidtech/io/DroidFile.java
Normal file
@ -0,0 +1,119 @@
|
||||
package net.droidtech.io;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public class DroidFile extends File{
|
||||
|
||||
private static final long serialVersionUID = 7311643678032690177L;
|
||||
private File file=null;
|
||||
|
||||
public DroidFile(String path){
|
||||
super(path);
|
||||
file=new File(path);
|
||||
}
|
||||
public DataInputStream getInputStream(){
|
||||
try {
|
||||
if(file.exists()&&!file.isDirectory()){
|
||||
DataInputStream in=new DataInputStream(new FileInputStream(file));
|
||||
return in;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public DataOutputStream getOutputStream(){
|
||||
try {
|
||||
if(file.exists()&&!file.isDirectory()){
|
||||
DataOutputStream out=new DataOutputStream(new FileOutputStream(file));
|
||||
return out;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void copyTo(File target){
|
||||
DroidFile file=new DroidFile(target.getAbsolutePath());
|
||||
if(file.createNewFile()){
|
||||
StreamWriter writer=new StreamWriter();
|
||||
writer.writeStream(this.getInputStream(),file);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean createNewFile(){
|
||||
try {
|
||||
File parentDir=new File(file.getParent());
|
||||
if(!parentDir.exists()){
|
||||
parentDir.mkdirs();
|
||||
}
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
return false;
|
||||
}
|
||||
return file.exists();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mkdir(){
|
||||
file.mkdirs();
|
||||
return file.exists();
|
||||
}
|
||||
|
||||
public String getMD5String(){
|
||||
byte[] md5data=getMD5Bytes();
|
||||
if(md5data!=null){
|
||||
return new BigInteger(1,md5data).toString(16);
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] getMD5Bytes(){
|
||||
DataInputStream in=this.getInputStream();
|
||||
if(in==null){
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
byte[] buffer=new byte[4096];
|
||||
MessageDigest md5=MessageDigest.getInstance("MD5");
|
||||
while(true){
|
||||
int count=in.read(buffer);
|
||||
if(count!=-1){
|
||||
md5.update(buffer,0,count);
|
||||
}else{
|
||||
in.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return md5.digest();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public DroidFile getChildFile(String name){
|
||||
if(!this.isDirectory()){
|
||||
return null;
|
||||
}
|
||||
return new DroidFile(this.getAbsolutePath()+File.separator+name);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user