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

原创 2006年06月01日 18:05:00

/**
 * 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;
 }

}

c#操作xml文档的通用类

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu...
  • zhujunxxxxx
  • zhujunxxxxx
  • 2014年01月19日 13:22
  • 2019

Java类型信息与用反射机制编写通用的Excel导入导出

Java类文件编译后,会在类中添加一个静态属性,这个属性就是Class类的实例,用于描述类型信息。描述类信息的架构图如: Class对象提供了一组方法,让我们可以方便获取类的属性,方法,构造方法...
  • liu_c_y
  • liu_c_y
  • 2016年07月03日 15:17
  • 3113

redis通用工具类

public class RedisUtil { private static final Logger LOGGER = Logger.getLogger(RedisUtil.class);...
  • qq7342272
  • qq7342272
  • 2015年10月21日 09:21
  • 9247

文件上传类,通用

package com.beijian.action; import com.beijian.common.util.FileUtil; import com.beijian.common.util...
  • tanyit
  • tanyit
  • 2015年04月28日 03:52
  • 812

文件上传类,通用

package com.beijian.action; import com.beijian.common.util.FileUtil; import com.beijian.common.util...
  • tanyit
  • tanyit
  • 2015年04月28日 03:53
  • 564

SQLite通用数据库类

using System; using System.Collections; using System.Collections.Specialized; using System.Data; usi...
  • CLeopard
  • CLeopard
  • 2014年10月28日 22:31
  • 1134

Hibernate通用Dao实现

Hibernate通用Dao实现@(Hibernate)[dao, hibernate, 反射]Hibernate通用Dao实现 BaseDao接口 BaseDao接口实现类 BaseDao接口pac...
  • q547550831
  • q547550831
  • 2016年12月02日 18:34
  • 3010

Java文件操作类封装

/*http://blog.csdn.net/tsyj810883979/article/details/6522669*/ import java.io.BufferedReader; ...
  • daditao
  • daditao
  • 2014年01月03日 14:27
  • 1167

Java学习日记8:文件上传工具类的实现和服务器给客户端发送图片

文件上传的工具类,服务器动态给客户端相应图片
  • Sleep_At_Home
  • Sleep_At_Home
  • 2017年03月15日 22:45
  • 718

JAVA文件上传工具类

package com.ccxe.trust.proselect.action; import java.io.File; import java.io.FileInputStream; impor...
  • dly120219891208
  • dly120219891208
  • 2014年06月26日 11:33
  • 1776
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:JAVA实现对文件操作的通用类
举报原因:
原因补充:

(最多只允许输入30个字)