常用的文件操作方法


import java.io.*;
import java.util.*;

/**
 * 文件管理
 * @author xjh
 */
public class FileManage {
 public FileManage() {
 }

 /**
  * 复制文件
  * @param oldPath String 被复制的文件
  * @param newPath String 复制的文件
  * @return -1表示失败,0表示已经存在文件,1表示复制成功
  */
 public int copyFile(String oldPath, String newPath) {
  try {
   int bytesum = 0;
   int byteread = 0;
   File oldfile = new File(oldPath);
   File newfile = new File(newPath);
   if (oldfile.exists()) {
    // 文件存在时
    if (newfile.exists()) {
     return 0;
    } else {
     InputStream inStream = new FileInputStream(oldPath); // 读入原文件
     FileOutputStream fs = new FileOutputStream(newPath);
     byte[] buffer = new byte[1444];
     while ((byteread = inStream.read(buffer)) != -1) {
      bytesum += byteread; // 字节数 文件大小
      System.out.println(bytesum);
      fs.write(buffer, 0, byteread);
     }
     inStream.close();
     fs.close();
    }
   }
   return 1;
  } catch (Exception e) {
   System.out.println("复制单个文件操作出错");
   e.printStackTrace();
   return -1;
  }
 }

 /**
  * 删除文件
  * @param filePathAndName 文件路径
  * @return 操作结果
  */
 public String delFile(String filePathAndName) {
  String strresult = "";
  try {
   String filePath = filePathAndName;
   filePath = filePath.toString();
   File myDelFile = new File(filePath);
   if (myDelFile.exists()) {
    boolean result = myDelFile.delete();
    if (result) {
     strresult = "删除文件成功!";
    } else {
     strresult = "删除文件操作失败!";
    }
   } else {
    strresult = "文件不存在";
   }
  } catch (Exception e) {
   strresult = "删除文件操作出错";
   e.printStackTrace();
  }
  return strresult;
 }

 /**
  * 得到文件目录下的所有文件及文件夹
  * @param path String 路径
  * @return ArrayList 文件及文件夹列表
  */
 public ArrayList getAllFilesInPath(String path) {
  ArrayList alAllFile = new ArrayList();
  File file = new File(path);
  if (!file.exists()) {
   return null;
  }
  File[] aryFile = file.listFiles();
  for (int i = 0; i < aryFile.length; i++) {
   alAllFile.add(aryFile[i]);
  }
  return alAllFile;
 }

 /**
  * 得到文件目录下的所有文件
  * @param path 路径
  * @return 文件列表
  */
 public ArrayList getAllFileInPath(String path) {
  ArrayList alAllFile = new ArrayList();
  File file = new File(path);
  if (!file.exists()) {
   return null;
  }
  if (!file.isDirectory()) {
   return null;
  }
  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()) {
    alAllFile.add(temp);
   }
  }
  return alAllFile;
 }

 /**
  * 得到文件夹下的所有文件名
  * @param path 路径
  * @return 文件名列表
  */
 public ArrayList getAllFileNameInPath(String path) {
  ArrayList alAllFile = new ArrayList();
  File file = new File(path);
  if (!file.exists()) {
   return null;
  }
  if (!file.isDirectory()) {
   return null;
  }
  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()) {
    alAllFile.add(temp.getName());
   }
  }
  return alAllFile;
 }

 /**
  * 在文件夹中是否存在某一文件
  * @param path 路径
  * @param filename 文件名
  * @return boolean true 存在该文件 false 不存在该文件
  */
 public boolean ifExistFileNameInPath(String path, String filename) {
  boolean haveFile = false;
  ArrayList alAllFile = getAllFileNameInPath(path);
  if (alAllFile != null && alAllFile.size() > 0) {
   for (int i = 0; i < alAllFile.size(); i++) {
    if (alAllFile.get(i).toString().equals(filename)) {
     haveFile = true;
     return haveFile;
    }
   }
  }
  return haveFile;
 }

 /**
  * 读文件内容
  * @param strFilePath 文件路径
  * @return 文件内容
  * @throws IOException 异常
  */
 public String readFile(String strFilePath) throws IOException {
  String strReturn = "";
  StringBuffer strline = new StringBuffer();
  BufferedReader reader = null;
  File file = null;
  FileReader freader = null;
  try {
   file = new File(strFilePath);// 需要指定文件路径
   freader = new FileReader(file);
   reader = new BufferedReader(freader);
   String strtemp = "";
   strtemp = reader.readLine();

   while (strtemp != null) {
    strtemp = reader.readLine();
    strline.append(strtemp + "/n"); // name
   }
  } catch (FileNotFoundException e) {
   System.err.println("没有找到文件");
  }
  strReturn = strline.toString();
  return strReturn;
 }

 /**
  * 取得上级目录
  * @param strFilePath 目录路径
  * @return 上级目录
  */
 public String getUpFoldPath(String strFilePath) {
  int pos = 0;
  pos = strFilePath.lastIndexOf('/');
  if (pos != -1) {
   return strFilePath.substring(0, pos);
  }
  pos = strFilePath.lastIndexOf('//');
  if (pos != -1) {
   return strFilePath.substring(0, pos);
  } else {
   return strFilePath;
  }
 }

 /**
  * 取得文件的后缀名
  * @param fileName 文件名
  * @return 文件的后缀名
  */
 public String getFileSuffix(String fileName) {
  int pos = 0;
  pos = fileName.lastIndexOf('.');
  if (pos != -1) {
   return fileName.substring(pos + 1);
  } else {
   return "";
  }
 }

 /**
  * 创建文件夹
  * @param foldPath 路径
  * @return 文件夹名
  */
 public String creatFlod(String foldPath) {
  File f = new File(foldPath);
  if (f.exists()) {
  } else {
   f.mkdirs();
  }
  return f.getName();
 }
}

原文地址:https://www.cnblogs.com/xiejava/p/15171497.html