Android 实现拷贝单个或多个文件和文件夹到另一个目录下,获取单个文件夹里面文件大小和单个文件夹下多个文件夹和文件的大小
程序员文章站
2024-03-09 10:04:35
...
在AndroidManifest.xml中声明权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
android6.0之后要在动态申请权限
//android6.0之后要动态获取权限
private void checkPermission(Activity activity) {
// Storage Permissions
final int REQUEST_EXTERNAL_STORAGE = 1;
String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
try {
//检测是否有写的权限
int permission = ActivityCompat.checkSelfPermission(DownLoadActivity.this,
"android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
// 没有写的权限,去申请写的权限,会弹出对话框
ActivityCompat.requestPermissions(DownLoadActivity.this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
**
* 复制单个文件
*
* @param oldPath$Name String 原文件路径+文件名 如:data/user/0/com.test/files/abc.txt
* @param newPath$Name String 复制后路径+文件名 如:data/user/0/com.test/cache/abc.txt
* @return <code>true</code> if and only if the file was copied;
* <code>false</code> otherwise
*/
public boolean copyFile(String oldPath$Name, String newPath$Name) {
try {
File oldFile = new File(oldPath$Name);
if (!oldFile.exists()) {
Log.e("--Method--", "copyFile: oldFile not exist.");
return false;
} else if (!oldFile.isFile()) {
Log.e("--Method--", "copyFile: oldFile not file.");
return false;
} else if (!oldFile.canRead()) {
Log.e("--Method--", "copyFile: oldFile cannot read.");
return false;
}
FileInputStream fileInputStream = new FileInputStream(oldPath$Name); //读入原文件
FileOutputStream fileOutputStream = new FileOutputStream(newPath$Name);
byte[] buffer = new byte[1024];
int byteRead;
//!=-1 也可以写成!=null,意思是读取的数据不为负数或者null就说明还没有读取完毕
while ((byteRead = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, byteRead);
}
fileInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 复制文件夹及其中的文件
*
* @param oldPath String 原文件夹路径 如:data/user/0/com.test/files
* @param newPath String 复制后的路径 如:data/user/0/com.test/cache
* @return <code>true</code> if and only if the directory and files were copied;
* <code>false</code> otherwise
*/
public boolean copyFolder(String oldPath, String newPath) {
try {
File newFile = new File(newPath);
if (!newFile.exists()) {
if (!newFile.mkdirs()) {
Log.e("--Method--", "copyFolder: cannot create directory.");
return false;
}
}
File oldFile = new File(oldPath);
String[] files = oldFile.list();
File temp;
for (String file : files) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file);
} else {
temp = new File(oldPath + File.separator + file);
}
if (temp.isDirectory()) { //如果是子文件夹
copyFolder(oldPath + "/" + file, newPath + "/" + file);
} else if (!temp.exists()) {
Log.e("--Method--", "copyFolder: oldFile not exist.");
return false;
} else if (!temp.isFile()) {
Log.e("--Method--", "copyFolder: oldFile not file.");
return false;
} else if (!temp.canRead()) {
Log.e("--Method--", "copyFolder: oldFile cannot read.");
return false;
} else {
FileInputStream fileInputStream = new FileInputStream(temp);
FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
byte[] buffer = new byte[1024];
int byteRead;
//!=-1 也可以写成!=null,意思是读取的数据不为负数或者null就说明还没有读取完毕
while ((byteRead = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, byteRead);
}
fileInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
//获取单个文件夹里面文件大小和单个文件夹下多个文件夹和文件的大小
/**
* 读取文件的大小
* 需要读取的文件名字,文件夹的名字
*/
public int file_length(String path){
int length = 0;
try {
//日志
File df = new File(path);
//读取文件
FileInputStream fileInputStream = new FileInputStream(df);
//获取大小,单位是B
length = fileInputStream.available();
} catch (IOException e) {
e.printStackTrace();
}
return length;
}
/**
* 获取指定文件夹大小
* @param f 文件夹的路径
* @return
* @throws Exception
*/
private long getFileSizes(File f) throws Exception
{
long size = 0;
File flist[] = f.listFiles();
for (int i = 0; i < flist.length; i++){
size =size + getFileSize(flist[i]);
}
return size;
}
/**
* 获取指定文件大小
* @param
* @return
* @throws Exception
*/
private long getFileSize(File file) throws Exception
{
long size = 0;
if (file.exists()){
FileInputStream fis = null;
fis = new FileInputStream(file);
size = fis.available();
}
else{
file.createNewFile();
Log.e("获取文件大小","文件不存在!");
}
return size;
}