package com.cheyoushuor.enamefile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ReNameFileName {
private static int COUNT = 0;
private static int NUM = 0;
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
readAndCopeFile("E:" + File.separator + "180");
}
/*
* 执行文件复制操作
*/
public static void copyFile(String filePath) {
File file = new File(filePath);
System.out.println(file.getName());
}
/**
* 读取文件并且复制文件到指定目录
*
* @param filepath
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static boolean readAndCopeFile(String filepath)
throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (file.isDirectory()) {
NUM++;
System.out.println(NUM + "Nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
System.out.println("\r\n");
//获取文件绝对路径
String filePath = readfile.getPath();
//System.out.println("path=" + filePath);
//获取原文件的名称
String fileName = readfile.getName();
//System.out.println("fileName = " + fileName);
//测试拼接的文件路径是否正确
//System.out.println("E:" + File.separator + "car" + File.separator + fileName);
//父文件夹的路径
String parentFileName = readfile.getParent();
File parentFile = new File(parentFileName);
//用于判断文件夹里面的文件到底有多少个。如果一个文件夹下多个文件,那么现实是多个文件的问价路径,下面两行为输出提示语句
if (filelist.length > 1) {
System.out.println(file.getPath()+"-----------------------------------------------------------------------------------------------------------------------------------");
}
//获取到扩展名前.在文件名中的位置
int beginIndex = fileName.indexOf(".");
//产生新文件明
String newFileName = parentFileName.substring(parentFileName.lastIndexOf("\\") + 1).substring(0, 4)
+ fileName.substring(beginIndex, fileName.length());
//System.out.println(newFileName);
//调用复制文件的代码
copyFile(filePath, "E:" + File.separator + "car" + File.separator + newFileName, false);
} else if (readfile.isDirectory()) {
++COUNT;
System.out.println(COUNT+"==================================================================================================");
if(!isFolerNull(readfile)) {
System.out.println(readfile.getPath() + "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
}
readAndCopeFile(filepath + "\\" + filelist[i]);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
return true;
}
public static boolean isFolerNull(File f) {
return f.list().length > 0;
}
/**
* 复制单个文件
*
* @param oldPath老文件名
* @param newPath新文件名
*/
public static void copyFile(String oldPath, String newPath) {
try {
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
/**
* 复制单个文件
*
* @param srcFileName 待复制的文件名
* @param destFileName 目标文件名
* @param overlay 如果目标文件存在,是否覆盖
* @return 如果复制成功,则返回true,否则返回false
*/
public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
//判断原文件是否存在
File srcFile = new File(srcFileName);
if (!srcFile.exists()) {
System.out.println("复制文件失败:原文件" + srcFileName + "不存在!");
return false;
} else if (!srcFile.isFile()) {
System.out.println("复制文件失败:" + srcFileName + "不是一个文件!");
return false;
}
//判断目标文件是否存在
File destFile = new File(destFileName);
if (destFile.exists()) {
//如果目标文件存在,而且复制时允许覆盖。
if (overlay) {
//删除已存在的目标文件,无论目标文件是目录还是单个文件
System.out.println("目标文件已存在,准备删除它!");
boolean success = destFile.delete();
if (success = false) {
System.out.println("复制文件失败:删除目标文件" + destFileName + "失败!");
return false;
}
} else {
System.out.println("复制文件失败:目标文件" + destFileName + "已存在!");
return false;
}
} else {
if (!destFile.getParentFile().exists()) {
//如果目标文件所在的目录不存在,则创建目录
System.out.println("目标文件所在的目录不存在,准备创建它!");
if (!destFile.getParentFile().mkdirs()) {
System.out.println("复制文件失败:创建目标文件所在的目录失败!");
return false;
}
}
}
//准备复制文件
int byteread = 0;//读取的位数
InputStream in = null;
OutputStream out = null;
try {
//打开原文件
in = new FileInputStream(srcFile);
//打开连接到目标文件的输出流
out = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
//一次读取1024个字节,当byteread为-1时表示文件已经读完
while ((byteread = in.read(buffer)) != -1) {
//将读取的字节写入输出流
out.write(buffer, 0, byteread);
}
System.out.println("复制单个文件" + srcFileName + "至" + destFileName + "成功!");
return true;
} catch (Exception e) {
System.out.println("复制文件失败:" + e.getMessage());
return false;
} finally {
//关闭输入输出流,注意先关闭输出流,再关闭输入流
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}