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 {
}
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的操作 新建文件 移动文件 复制文件 删除文件
- java nio: walkFileTree实现文件夹复制移动删除
- java中File文件的创建,删除,复制,移动,以及目录文件的删除等等,主要是文件及文件夹的操作
- java 的 文件、文件夹 的建立、删除、复制以及移动等功能 操作
- java文件整理(二)----对文件工具类(复制,删除,移动)
- java 实现文件/文件夹复制、删除、移动(二)
- java IO操作 (读写、追加、删除、移动、复制)
- Java Io文件操作(读写、追加、删除、移动、复制、修改)
- Java中实现复制文件到文件,复制文件到文件夹,复制文件夹到文件,删除文件,删除文件夹,移动文件,移动文件夹的工具类
- java IO操作 (读写、追加、删除、移动、复制等)
- java IO操作 (读写、追加、删除、移动、复制等)
- java 对文件操作 文件(夹)新建、复制、删除、移动
- 学习笔记-java实现复制、移动、删除文件夹
- java文件整理(二)----对文件工具类(复制,删除,移动)
- java文件整理(二)----对文件工具类(复制,删除,移动)
- 【转载】Java实现文件夹的移动、内容复制、删除代码
- java复制文件删除文件 移动文件 复制文件夹等
- Java实现文件夹的复制、移动、删除代码
- java文件和文件夹复制、删除、移动操作
- file 文件的操作 移动 、删除 、复制 操作