cfile.java

来自「java开发的办公系统 1.系统管理 (地区管理,部门管理,菜单管理,用户管理」· Java 代码 · 共 244 行

JAVA
244
字号

package com.vere.util;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.util.Date;
import java.util.Random;


public class CFile {

  public CFile() {
    //
  }


  public static boolean fileExists(String _sPathName) {
    File file = new File(_sPathName);
    if(file.isFile())
    	return file.exists();
    return false;
  }


  public static String extractFileName(String _sFilePathName) {
    int nPos = _sFilePathName.lastIndexOf(File.separatorChar);
    return _sFilePathName.substring(nPos + 1);
  }


  public static String extractFileExt(String _sFilePathName) {
    int nPos = _sFilePathName.lastIndexOf('.');
    return (nPos >= 0 ? _sFilePathName.substring(nPos + 1) : "");
  }

  public static String extractFilePath(String _sFilePathName) {
    int nPos = _sFilePathName.lastIndexOf(File.separatorChar);
    return (nPos >= 0 ? _sFilePathName.substring(0, nPos + 1) : "");
  }


  public static String toAbsolutePathName(String _sFilePathName) {
    File file = new File(_sFilePathName);
    return file.getAbsolutePath();
  }


  public static String extractFileDrive(String _sFilePathName) {
    int nPos;
    int nLen = _sFilePathName.length();


    if ( (nLen > 2) && (_sFilePathName.charAt(1) == ':')) {
      return _sFilePathName.substring(0, 2);
    }


    if ( (nLen > 2) && (_sFilePathName.charAt(0) == File.separatorChar) &&
        (_sFilePathName.charAt(1) == File.separatorChar)) {
      nPos = _sFilePathName.indexOf(File.separatorChar, 2);
      if (nPos >= 0) {
        nPos = _sFilePathName.indexOf(File.separatorChar, nPos + 1);
      }
      return (nPos >= 0 ? _sFilePathName.substring(0, nPos) : _sFilePathName);
    }
    else {
      return "";
    }
  }

  public static boolean deleteFile(String _sFilePathName) {
    File file = new File(_sFilePathName);
    if(file.isFile())
    	return file.delete();
    return false;
  }
  


  public static boolean makeDir(String _sDir, boolean _bCreateParentDir) {
    File file = new File(_sDir);
    if (_bCreateParentDir) {
      return file.mkdirs();
    }
    else {
      return file.mkdir();
    }

  }

  public static boolean deleteDir(String _sDir) {
    return deleteDir(_sDir, false);
  }

  public static boolean deleteDir(String _sDir, boolean _bDeleteChildren) {
    File file = new File(_sDir);
    if (!file.exists()) {
      return false;
    }

    if (_bDeleteChildren) {
      File[] files = file.listFiles();
      for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
          deleteDir(files[i].getAbsolutePath(), _bDeleteChildren);
        }
        else {
          files[i].delete();
        }
      }
    }
    return file.delete();

  }


  public static String readFile(String _sFileName) throws Exception {
    String sResult = "";
    String encoding = "gb2312";
    InputStreamReader reader = null;
    BufferedReader br = null;
    try {
      reader = new InputStreamReader(new FileInputStream(
          _sFileName), encoding);
       br = new BufferedReader(reader);
      String sLine = "";
      while ( (sLine = br.readLine()) != null) {
        sResult = sResult + sLine + "\n";
      }
    }

   catch (IOException x) {

    }
    finally {
      if (br != null){
        br.close();
      }
      if (reader != null){
        reader.close();
      }
    }

    return sResult;
  }


  public static boolean writeFile(String _sFileName, String _sFileContent) throws Exception {
    boolean bRet = false;
    String encoding = "gb2312";
    OutputStreamWriter out = null;
    PrintWriter pw = null;
    try {
      out = new OutputStreamWriter(new FileOutputStream(
          _sFileName), encoding);
      pw = new PrintWriter(out);
      pw.println(_sFileContent);
      bRet = true;
    }
    catch (Exception ex) {

    }    finally {
      if (pw != null){
        pw.close();
      }
      if (out != null){
        out.close();
      }
    }

    return bRet;
  }



  public static boolean appendFile(String _sFileName, String _sAddContent) throws
    Exception {
    boolean bResult = false;
    try {
      RandomAccessFile raf = new RandomAccessFile(_sFileName, "rw");
      raf.seek(raf.length());
      raf.writeBytes(_sAddContent);
      raf.close();
      bResult = true;
    }
    catch (Exception ex) {

    }
    return bResult;
  }


  public static boolean copyFile(String _sSrcFile, String _sDstFile) throws Exception {
    boolean bResult = false;
    int buffer;
    try {
      FileInputStream fis = new FileInputStream(_sSrcFile);
      FileOutputStream fos = new FileOutputStream(_sDstFile);
      DataInputStream dis = new DataInputStream(fis);
      DataOutputStream dos = new DataOutputStream(fos);
      while ( (buffer = dis.read()) != -1) {
        dos.write(buffer);
      } //end while
      dis.close();
      dos.close();
      fis.close();
      fos.close();
      bResult = true;
    }
    catch (FileNotFoundException ex) {

    }
    catch (IOException ex) {

    }
    return bResult;
  }


  public static String generateFileName(String fileType, String extName) {
    if (fileType == null) {
      fileType = "unkonwn";
    }
    String info = fileType + "_";
    Random random = new Random();
    Date myDate = new Date();
    info += (new Long(myDate.getTime())).toString();
    info += (new Integer(random.nextInt(1000))).toString();
    info += "." + extName;
    return info;
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?