关闭

JAVA实现对文件操作的通用类

926人阅读 评论(1) 收藏 举报

/**
 * Script Name : <b>FileRobot</b> <br>
 * Description : The operation on file <br>
 * Type : Java <br>
 * Generated : <b>2006-5-26 13:49:00</b> <br>
 * Original Host : WinNT Version 5.1 Build 2600 (S)
 *
 * @since 2006/05/26
 * @author Jaytse
 */
public class FileRobot {

 /**
  * Write some info to a file
  *
  * @param filePath
  *            the filepath of the file
  * @param fileName
  *            the name of the file
  * @param contents
  *            the information wanted to writed in
  * @throws IOException
  */
 public static void writeFile(String filePath, String fileName,
   String[] contents) throws IOException {
  FileWriter fw = new FileWriter(filePath + fileName);
  PrintWriter out = new PrintWriter(fw);
  for (int i = 0; i < contents.length; i++) {
   out.write(contents[i]);
   out.println();
   out.flush();
  }
  fw.close();
  out.close();
 }

 /**
  * Write some info to a file
  *
  * @param filePath
  *            the filepath of the file
  * @param fileName
  *            the name of the file
  * @param content
  *            the information wanted to writed in
  * @throws IOException
  */
 public static void writeFile(String filePath, String fileName,
   String content) throws IOException {
  String[] contents = new String[1];
  contents[0] = content;
  writeFile(filePath, fileName, contents);
 }

 /**
  * Create and Delete a file
  *
  * @param filePath
  *            the path of file
  * @param fileName
  *            the name of file
  * @return if operation success, return true
  * @throws IOException
  */
 public static boolean createAndDeleteFile(String filePath, String fileName)
   throws IOException {
  boolean result = false;
  File file = new File(filePath, fileName);
  if (file.exists()) {
   file.delete();
   result = true;
   System.out.println("File has been Delete!");
  } else {
   file.createNewFile();
   result = true;
   System.out.println("File has been Create!");
  }
  return result;
 }

 /**
  * Create and Delete a folder
  *
  * @param folderName
  *            the folder name
  * @param filePath
  *            the fold path
  * @return if operation success, return true
  */
 public static boolean createAndDeleteFolder(String folderName,
   String filePath) {
  boolean result = false;
  try {
   File file = new File(filePath + folderName);
   if (file.exists()) {
    file.delete();
    System.out.println("Folder exists and it has been delete!");
    result = true;
   } else {
    file.mkdir();
    System.out
      .println("Folder does not exist and it has been created!");
    result = true;
   }
  } catch (Exception ex) {
   result = false;
   System.out.println("CreateAndDeleteFolder is error:" + ex);
  }
  return result;
 }

 /**
  * Create a file with its name
  * @param fileName the file name
  */
 public static void makeFile(String fileName) {
  String dir = fileName.substring(0, fileName.lastIndexOf("//"));
  String name = fileName.substring(fileName.lastIndexOf("//") + 1,
    fileName.length());
  makeFile(dir, name);
 }

 /**
  * Create a file with its name, and its directory name
  * @param dirName the directory name
  * @param fileName the file name
  */
 public static void makeFile(String dirName, String fileName) {
  try {
   Stack stack = new Stack();
   String old = dirName;
   String hhh = old;
   while (hhh.indexOf("//") != -1) {
    if (!hhh.equals(old)) {
     stack.push(hhh);
    }
    stack.push(hhh.substring(hhh.lastIndexOf("//") + 1));
    hhh = hhh.substring(0, hhh.lastIndexOf("//"));

   }
   stack.push(hhh);
   int size = stack.size() / 2;
   for (int i = 0; i < size; i++) {
    File file = new File((String) stack.pop() + "//"
      + (String) stack.pop());
    file.mkdir();
   }

   File file = new File(dirName + "//" + fileName);
   file.createNewFile();

  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /**
  * Print folder and file list at the filepath
  *
  * @param filePath
  *            the destinated filepath
  */
 public static void readFolderByFile(String filePath) {
  File file = new File(filePath);
  File[] tempFile = file.listFiles();
  for (int i = 0; i < tempFile.length; i++) {
   if (tempFile[i].isFile()) {
    System.out.println("File : " + tempFile[i].getName());
   }
   if (tempFile[i].isDirectory()) {
    System.out.println("Directory : " + tempFile[i].getName());
   }
  }
 }

 /**
  * chech whether the file content is empty
  *
  * @param filePath
  *            the path of the file
  * @param fileName
  *            the name of the file
  * @return when the content is empty, return true
  * @throws IOException
  */
 public static boolean fileIsNull(String filePath, String fileName)
   throws IOException {
  boolean result = false;
  FileReader fr = new FileReader(filePath + fileName);
  if (fr.read() == -1) {
   result = true;
   System.out.println(fileName + " has no data!");
  } else {
   System.out.println(fileName + " has some data!");
  }
  fr.close();
  return result;
 }

 /**
  * Get the content of the file
  *
  * @param filePath
  *            the path of the file
  * @param fileName
  *            the name of the file
  * @return the content of the file
  * @throws IOException
  */
 public static String readAllFile(String filePath, String fileName)
   throws IOException {
  StringBuffer buffer = new StringBuffer();
  FileReader fr = new FileReader(filePath + fileName);
  int count = fr.read();
  while (count != -1) {
   buffer.append((char) count);
   count = fr.read();
   if (count == 13) {
    fr.skip(1);
   }
  }
  fr.close();
  return buffer.toString();
 }

 /**
  * Get the content of the file
  *
  * @param filePath
  *            the path of the file
  * @param fileName
  *            the name of the file
  * @return the file content
  * @throws IOException
  */
 public static String readLineFile(String filePath, String fileName)
   throws IOException {
  return readLineFile(filePath + fileName);
 }

 /**
  * Get the content of the file
  * @param fileFullName the file name
  * @return the file content
  * @throws IOException
  */
 public static String readLineFile(String fileFullName) throws IOException {
  StringBuffer buffer = new StringBuffer();
  FileReader fr = new FileReader(fileFullName);
  BufferedReader br = new BufferedReader(fr);
  String line = br.readLine();
  while (line != null) {
   buffer.append(line);
   line = br.readLine();
  }
  br.close();
  fr.close();
  return buffer.toString();
 }
 
 /**
  * Get files for process
  * @param file the directory name
  * @param type the file type
  * @return the sub-directory and the files
  */
 public static File[] getFilesWithFilter(File file, String type){
  File[] list = file.listFiles();
  Vector v = new Vector();
  for (int i = 0; i < list.length; i++) {
   if(list[i].isDirectory()){
    if(checkDirFile(list[i], type)){
     v.addElement(list[i]);
    }
   }else{
    if(list[i].getName().endsWith("." + type)){
     v.addElement(list[i]);
    }
   }
  }
  File[] Result = new File[v.size()];
  for (int i = 0; i < Result.length; i++) {
   Result[i] = (File) v.elementAt(i);
  }
  return Result;
 }
 
 /**
  * Check whether a dir contains a file
  * @param file directory name
  * @param type file name
  * @return true if yes
  */
 public static boolean checkDirFile(File file, String type){
  boolean bResult = false;
  
  if(file.isDirectory()){
   File[] list = file.listFiles();
   for(int i = 0 ; i<list.length; i++){
    if(list[i].isDirectory()){
     bResult |= checkDirFile(list[i], type);
    }else{
     bResult |= list[i].getName().endsWith("." + type);
    }
   }

  }
  
  return bResult;
 }

}

0
0

猜你在找
【直播】计算机视觉原理及实战—屈教授
【套餐】深度学习入门视频课程—唐宇迪
【套餐】Hadoop生态系统零基础入门--侯勇蛟
【套餐】嵌入式Linux C编程基础--朱有鹏
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】机器学习之凸优化——马博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:166250次
    • 积分:2421
    • 等级:
    • 排名:第15255名
    • 原创:68篇
    • 转载:30篇
    • 译文:0篇
    • 评论:46条
    最新评论
    我的相册