您的位置:首页 > 编程语言 > Java开发

java——File复制、移动、删除

2017-12-13 15:58 211 查看
package com.file.myfile;

import android.util.Log;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.List;

public class MyFile {

private static final String TAG = "MyFile";
/**
* deleteFile
* @param filePath
* @return boolean
*/
public boolean deleteFile(String filePath) {
if(filePath == null) {
return false;
}

File file = new File(filePath);
if(file.exists()) {
return file.delete();
}
Log.i(TAG,"file no exist filePath = " + filePath    );
return false;
}

/**
* deleteFileRecursively
* @param filePath
* @return List<String>
*/
public List<String> deleteFileRecursively(String  filePath) {

List<String> result = null;
if (filePath == null) return result;

File file = new File(filePath);
if (!file.exists()) return result;

if (file.isDirectory()) {
for (String fileName : file.list()) {
File item = new File(file, fileName);
if (item.isDirectory()) {
result.addAll(deleteFileRecursively(item.getPath()));
} else {
if (!item.delete()) {
Log.d(TAG , " file's path is: " + item.getPath());
result.add(item.getPath());
}
}
}
if (!file.delete()) {
Log.d(TAG, "directories' path is: " + file.getPath());
result.add(file.getPath());
}

} else {
if (!file.delete()) {
Log.d(TAG, " its path is: " +  file.getPath());
result.add(file.getPath());
}
}
return result;
}

/**
* copyMvFile
* @param fromFile
* @param toFile
* @param action  0 移动  1 复制
* @return List<String>
*/
public List<String> copyMvFile(List<String> fromFile,List<String> toFile,int action) {
List<String> result = null;
if (fromFile.isEmpty() || toFile.isEmpty() || (fromFile.size() == toFile.size() ) )
return  result;

for (int i = 0 ; i < fromFile.size(); i++) {
String fFileName = fromFile.get(i);
String tFileName = toFile.get(i);

if (!copyMvFileDir(fFileName,tFileName,action)) {
result.add(fFileName);
}
}
return result;
}

/**
* copyMvFileDir
* @param fromFileDir
* @param toFileDir
* @param action  0 移动  1 复制
* @return boolean
*/
public boolean copyMvFileDir(String fromFileDir,String toFileDir,int action) {

if (fromFileDir == null || toFileDir == null ) return false;

File fFileDir = new File(fromFileDir);

if (fFileDir.isDirectory()) {
File tFileDir = new File(toFileDir);

if (!tFileDir.exists()) {
tFileDir.mkdirs();
}

for (String fileName : fFileDir.list()) {
File item = new File(fFileDir, fileName);
if (item.isDirectory()) {
copyMvFileDir(item.getPath() + "/",toFileDir + item.getName() + "/",action);
} else {
if (!CopyMvFile(item.getPath(),toFileDir + item.getName(),action )) {
Log.d(TAG , " file's path is: " + item.getPath());
return false;
}
}
}
} else {
if (!CopyMvFile(fromFileDir,toFileDir + fFileDir.getName(),action )) {
Log.d(TAG, " its path is: " +  fFileDir.getPath());
return false;
}
}
return true;
}

/**
* CopyMvFile
* @param fromFile
* @param toFile
* @param action  0 移动  1 复制
* @return boolean
*/
public boolean CopyMvFile(String fromFile, String toFile,int action ) {

if (fromFile == null || toFile == null) return false;

File fFile = new File(fromFile);
if (!fFile.exists()) return false;

boolean resultCopy = false;
boolean resultDel = true;

if (action == 0) {
resultCopy = copyFileStream(fromFile,toFile);
resultDel = fFile.delete();
} else if (action == 1) {
resultCopy =  copyFileStream(fromFile,toFile);
}
return (resultCopy & resultDel);
}

/**
* copyFileStream
* @param fromFile
* @param toFile
* @return boolean
*/
public boolean copyFileStream(String fromFile, String toFile) {
try {
InputStream fosfrom = new FileInputStream(fromFile);
OutputStream fosto = new FileOutputStream(toFile);
byte bt[] = new byte[1024];
int c;
while ((c = fosfrom.read(bt)) > 0) {
fosto.write(bt, 0, c);
}
fosfrom.close();
fosto.close();
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java file 复制 移动 删除