//创建文件
public static boolean createFile(File file, boolean deleteOnExists) throws IOException {
if ( file . exists ()) {
if ( deleteOnExists ){
deleteFile ( file );
} else {
return true ;
}
}
File parent = file . getParentFile ();
if ( parent != null && ! parent . exists ()) {
parent . mkdirs ();
}
return file . createNewFile ();
}
//删除文件
public static boolean deleteFile ( File path ) {
boolean result = true ;
if ( path . exists ()) {
if ( path . isDirectory ()) {
File [] files = path . listFiles ();
for ( int i = 0 , count = files . length ; i < count ; i ++) {
result &= deleteFile ( files [ i ]);
}
result &= path . delete (); // Delete empty directory.
} else {
result &= path . delete ();
}
return result ;
} else {
return false ;
}
}
//删除文件夹内文件
public static boolean deleteInternalFilr ( File path ){
boolean result = true ;
if ( path . exists ()){
if ( path . isDirectory ()){
File [] files = path . listFiles ();
for ( int i = 0 , count = files . length ; i < count ; i ++) {
result &= deleteFile ( files [ i ]);
}
}
}
return result ;
}
//安装文件
public static boolean installFile ( File path , Activity context ){
boolean result = true ;
try {
Intent intent = new Intent ( Intent . ACTION_VIEW );
intent . setDataAndType ( Uri . fromFile ( path ), "application/vnd.android.package-archive" );
context . startActivityForResult ( intent , 1 );
} catch ( Exception ex ){
ex . printStackTrace ();
result = false ;
}
return result ;
}
//复制文件
public static boolean copyFile ( String savePath , String sourcePath ){
try {
FileInputStream fis = new FileInputStream ( new File ( sourcePath ));
FileOutputStream fos = new FileOutputStream ( new File ( savePath ));
byte [] temp = new byte [ 1024 ];
int ch ;
while (( ch = fis . read ( temp ))!=- 1 ){
fos . write ( temp , 0 , ch );
}
} catch ( Exception e ){
e . printStackTrace ();
return false ;
}
return true ;
}