整理JAVA File文件操作,读与拷贝
/**
*
* Description: 根据路径将文件转换成字节数组
*
* @param
* @return byte[]
* @throws
*/
public static byte[] getBytes(String filePath){
byte[] buffer = null;
try {
if(filePath != null && !"".equals(filePath)){
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
/**
* 得到文件的扩展名.
*
* @param fileName
*
*/
public static String getExtension(String fileName) {
if (fileName != null) {
int i = fileName.lastIndexOf('.');
if (i > 0 && i < fileName.length() - 1) {
return fileName.substring(i + 1).toLowerCase();
}
}
return "";
}
/**
* 获得文件的前缀名.
*/
public static String getPrefix(String fileName) {
if (fileName != null) {
fileName = fileName.replace('\\', '/');
if (fileName.lastIndexOf("/") > 0) {
fileName = fileName.substring(fileName.lastIndexOf("/") + 1,
fileName.length());
}
int i = fileName.lastIndexOf('.');
if (i > 0 && i < fileName.length() - 1) {
return fileName.substring(0, i);
}
}
return "";
}
/**
* 得到文件的短路径, 不包括目录.
*
*/
public static String getShortFileName(String fileName) {
if (fileName != null) {
String oldFileName = new String(fileName);
fileName = fileName.replace('\\', '/');
if (fileName.endsWith("/")) {
int idx = fileName.indexOf('/');
if (idx == -1 || idx == fileName.length() - 1) {
return oldFileName;
} else {
return oldFileName
.substring(idx + 1, fileName.length() - 1);
}
}
if (fileName.lastIndexOf("/") > 0) {
fileName = fileName.substring(fileName.lastIndexOf("/") + 1,
fileName.length());
}
return fileName;
}
return "";
}
/**
* 删除文件
*/
public static void delFile(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
File myDelFile = new File(filePath);
myDelFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除文件夹
*/
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); // 删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
if (myFilePath.delete()) { // 删除空文件夹
System.out.println("删除文件夹" + folderPath + "操作 成功执行");
} else {
System.out.println("删除文件夹" + folderPath + "操作 执行失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除文件夹里面的所有文件
*
*/
public static void delAllFile(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
// delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
delFolder(path + File.separatorChar + tempList[i]);// 再删除空文件夹
}
}
}
/**
* 复制单个文件
*
*/
public static void copyFile(String oldPath, String newPath) {
InputStream inStream = null;
FileOutputStream fs = null;
try {
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) {
// 文件存在时
inStream = new FileInputStream(oldPath); // 读入原文件
fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteread);
}
}
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
} finally{
try {
if(inStream != null){
inStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fs != null){
fs.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 复制整个文件夹内容
*
*/
public static void copyFolder(String oldPath, String newPath) {
FileInputStream input = null;
FileOutputStream output = null;
try {
(new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File a = new File(oldPath);
String[] file = a.list();
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}
if (temp.isFile()) {
input = new FileInputStream(temp);
output = new FileOutputStream(newPath
+ "/" + (temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
}
if (temp.isDirectory()) {
// 如果是子文件夹
copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
}
}
System.out.println("复制文件夹操作 成功执行");
} catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
} finally{
try {
if(output != null){
output.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(input != null){
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 移动文件到指定目录
*
* @param oldPath
* String 如:c:/fqf.txt
* @param newPath
* String 如:d:/fqf.txt
*/
public static void moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
/**
* 移动文件到指定目录
*
*/
public static void moveFolder(String oldPath, String newPath) {
copyFolder(oldPath, newPath);
delFolder(oldPath);
}
/**
* Description: 判断目录是否存在,不存在就创建
*
* @param
* @return void
*/
public static void existDirectory(String dir) {
File file = new File(dir);
if (!file.exists()) {
file.mkdirs();
}
}
/**
* 根据给定文件查询文件格式
* @param fileName 文件
* @return 文件格式 如.jpg,.pdf等由.+格式组成
*/
public static String getFileFormat(String fileName){
//文件格式
String fileFormat="";
if (fileName != null && !"".equals(fileName)) {
fileName = fileName.replace('\\', '/');
if (fileName.lastIndexOf(".") > 0) {
fileFormat = fileName.substring(fileName.lastIndexOf(".") ,
fileName.length());
}
return fileFormat;
}
return fileFormat;
}
/**
* 更改根据文件制定格式提示
* @param str 被替换字符串 如:/fdakd/20140519163615843.jpg
* @param replaceStr 如"\\"
* @return
*/
public static String changeStrByFormat(String str,String regex,String replaceStr){
String returnStr="";
if(str!=null && !"".equals(str)){
if(replaceStr!=null && !"".equals(replaceStr)){
returnStr=str.replaceAll(regex, replaceStr);
return returnStr;
}else{
returnStr=str;
}
}
return returnStr;
}