package tms.com.TmsFile;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* 根据文件名称复制文件到指定文件夹下。
* @author 2500216
*
*/
public class TmsFile {
/**
* all file cope
*
* @param filePath
* @param targetH
* @param targetK
* @return
* @throws IOException
*/
public void copeAllFile(String filePath, String targetH, String targetK)
throws IOException {
File file = new File(filePath);
System.out.println(file.getPath());
if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filePath + "\\" + filelist[i]);
// 如果是文件
if (!readfile.isDirectory()) {
String type = isType(readfile);
if (type.equals("H")) {
File targetFile = new File(targetH + readfile.getName());
copyFile(readfile, targetFile);
} else if (type.equals("K")) {
File targetFile = new File(targetK + readfile.getName());
copyFile(readfile, targetFile);
}
} else if (readfile.isDirectory()) {
copeAllFile(readfile.getPath(), targetH, targetK);
}
}
}
}
/**
* 判断是那个公司的
*
* @param file
* @return
*/
public String isType(File file) {
String type = file.getName().substring(0, 3);
String typeResult = "";
if ("KNA".equals(type)) {
typeResult = "K";
} else if ("KMH".equals(type)) {
typeResult = "H";
}
return typeResult;
}
/**
* 把文件复制到指定路径下
* @param sourceFile
* @param targetFile
* @throws IOException
*/
public static void copyFile(File sourceFile, File targetFile)
throws IOException {
System.out.println("readfile =" + sourceFile.getPath()
+ "-----> targetFile =" + targetFile.getPath());
BufferedInputStream inBuff = null;
BufferedOutputStream outBuff = null;
try {
// 新建文件输入流并对它进行缓冲
inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
// 新建文件输出流并对它进行缓冲
outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
// 缓冲数组
byte[] b = new byte[1024 * 5];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
} finally {
// 关闭流
if (inBuff != null)
inBuff.close();
if (outBuff != null)
outBuff.close();
}
}
/**
* 删除指定路径下的文件。
*
* @param filePath
* @return
*/
public void deleteFile(File fs) {
System.out.println("delete file name:" + fs.getName());
fs.delete();
}
/**
* 删除文件
*
* @param filePath
* @throws IOException
*/
public List<String> deleteAllFile(String filePath) throws IOException {
List<String> fileList = new ArrayList<String>();
File file = new File(filePath);
System.out.println(file.getPath());
if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filePath + "\\" + filelist[i]);
// 如果是文件
if (!readfile.isDirectory()) {
if (isDelete(readfile)) {
fileList.add(readfile.getName() + "====>"
+ readfile.getPath());
deleteFile(readfile);
}
} else if (readfile.isDirectory()) {
// 如果是目录:递归调用
deleteAllFile(readfile.getPath());
}
}
}
return fileList;
}
/**
* 是否删除(删除文件名中是11月的)
*
* @param file
* @return
*/
public boolean isDelete(File file) {
boolean isDelete = false;
String fileNameSub = file.getName().substring(22, 24);
// System.out.println("fileNameSub="+fileNameSub);
if (fileNameSub.equals("11")) {
isDelete = true;
} else {
isDelete = false;
}
return isDelete;
}
/**
* 真实拷贝
*
* @throws IOException
*/
public void startCope() throws IOException {
// 真实路径
String sourceFilePath = "E:\\AutoeverWorkFile\\tms\\sbs\\147\\log_vcrm\\";
String targetH = "E:\\AutoeverWorkFile\\tms\\sbs\\H\\";
String targetK = "E:\\AutoeverWorkFile\\tms\\sbs\\K\\";
copeAllFile(sourceFilePath, targetH, targetK);
}
/**
* test拷贝
*
* @throws IOException
*/
public void startCopeTest() throws IOException {
String sourceFilePath = "E:\\AutoeverWorkFile\\tms\\sbs\\vcrmtest\\";
String targetH = "E:\\AutoeverWorkFile\\tms\\sbs\\Htest\\";
String targetK = "E:\\AutoeverWorkFile\\tms\\sbs\\Ktest\\";
/*Map<String,String> targets=new HashMap();
targets.put("H", targetH);
targets.put("K", targetK);*/
copeAllFile(sourceFilePath, targetH, targetK);
}
public static void main(String[] args) throws IOException {
TmsFile tmsFile = new TmsFile();
// **true
// cope**************************************************************
// tmsFile.startCope();
// **test
// cope**************************************************************
//tmsFile.startCopeTest();
// 删除11月份的。
// tmsFile.deleteAllFile("E:\\AutoeverWorkFile\\tms\\sbs\\K\\");
// ****************************************************************
// 测试路径
}
}
java 文件复制,删除
最新推荐文章于 2020-02-23 11:55:27 发布