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

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

file文件操作通用类

C++学习:一个通用ini配置文件操作类

在windows平台下,简单的程序可以通过ini文件实现简单的配置,简单适用。在win32 sdk还提供了相应的api来读取修改ini文件。ini配置文件格式为: [section] key=st...

VC++文件操作通用类

通用文件操作类的设计

 转自 朱金灿        Windows环境下读写文件一般有下列有下面几种方式:C语言的文件操作函数,如fopen函数等,C++的I/O流库,Win32 API的文件操作函数,如Cre...

Java文件操作类

  • 2012-08-12 18:01
  • 95KB
  • 下载

mapgis6.7二次开发vc6_demo 之三(文件操作通用函数)

mapgis6.7二次开发vc6_demo 之三(文件操作通用函数) //FileOpClass.cpp文件内容如下所示: //////////////////////////////////...
  • hsg77
  • hsg77
  • 2012-04-18 17:58
  • 892

java 文件操作类

文件操作类(java)

  • 2015-09-05 09:59
  • 17KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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