⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileio.java

📁 java web模拟的简单的一个网络硬盘
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.dark.comm.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletContext;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import java.util.Vector;
import java.util.ArrayList;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;

/**
 * <p>文件处理公共类,包含一些图片文件的处理方法</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * @author dark.xie
 * @version 1.0
 */
public class FileIO {

  private static final Log logger = LogFactory.getLog(FileIO.class);

  /**
   * readFile(String filePath)
   * 通过文件路径以字节方式读取文件内容
   * @param filePath String
   * @return String
   */
  public static String readFile(String filePath) {
    String info = "";
    try {
      File f = new File(filePath);
      if (f.exists()) {
        FileInputStream bw = new FileInputStream(f);
        int len = bw.available();
        byte[] str = new byte[len];
        if (bw.read(str) == -1) {
          info = "";
        }
        else {
          info = new String(str);
        }
        bw.close();
        bw = null;
      }
      f = null;
    }
    catch (IOException e) {
      logger.error(e);
    }
    return info;
  }

  /**
   * readFile(String filePath, String charset)
   * 通过文件路径以字节方式按照指定的编码读取文件内容
   * @param filePath String
   * @param charset String
   * @return String
   */
  public static String readFile(String filePath, String charset) {
    String info = "";
    try {
      File f = new File(filePath);
      if (f.exists()) {
        FileInputStream bw = new FileInputStream(f);
        int len = bw.available();
        byte[] str = new byte[len];
        if (bw.read(str) == -1) {
          info = "";
        }
        else {
          info = new String(str, charset);
        }
        bw.close();
        bw = null;
      }
      f = null;
    }
    catch (IOException e) {
      logger.error(e);
    }
    return info;
  }

  /**
   * writeFile(String msg, String filePath)
   * 以字节的方式写文件
   * @param msg String
   * @param filePath String
   */
  public static void writeFile(String msg, String filePath) {
    try {
      File file = new File(filePath);
      if (file.exists()) {
        file.delete();
      }
      FileOutputStream wf = new FileOutputStream(filePath);
      wf.write(msg.getBytes());
      wf.close();
      file = null;
      wf = null;
    }
    catch (IOException e) {
      logger.error(e);
    }
  }

  /**
   * writeFile(String msg, String filePath, String charset)
   * 以字节的方式按照编码方式写文件内容
   * @param msg String
   * @param filePath String
   * @param charset String
   */
  public static void writeFile(String msg, String filePath, String charset) {
    try {
      File file = new File(filePath);
      if (file.exists()) {
        file.delete();
      }
      FileOutputStream wf = new FileOutputStream(filePath);
      wf.write(msg.getBytes(charset));
      wf.close();
      file = null;
      wf = null;
    }
    catch (IOException e) {
      logger.error(e);
    }
  }

  /**
   * makeDir(String directoryName)
   * 无条件建立一个目录
   * @param directoryName String
   * @return boolean
   */
  public static
      boolean makeDir(String directoryName) {
    boolean flag = false;
    try {
      File aFile = new File(directoryName);
      aFile.mkdir();
      flag = true;
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return flag;
  }

  /**
   * cutFile(String sourceDirectoryName,String sourceFileName,
              String DestDirectoryName,String DestFileName)
           把本地文件cut到另一个目录
   * @param sourceDirectoryName String
   * @param sourceFileName String
   * @param DestDirectoryName String
   * @param DestFileName String
   * @throws IOException
   * @return boolean
   */
  public static
      boolean cutFile(String sourceDirectoryName, String sourceFileName,
                      String DestDirectoryName, String DestFileName) throws
      java.io.IOException {
    boolean flag = false;
    try {
      File aFile = new File(sourceDirectoryName + sourceFileName);
      aFile.renameTo(new File(DestDirectoryName + DestFileName));
      aFile.delete();
      flag = true;
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return flag;
  }

  /**
   * cutFiles(String sourceDirectoryName,
   *                   String DestDirectoryName,Vector fileNames)
   *  把本地多个文件cut到另一个目录
   * @param sourceDirectoryName String
   * @param destDirectoryName String
   * @param fileNames Vector
   * @throws IOException
   * @return boolean
   */
  public static
      boolean cutFiles(String sourceDirectoryName, String destDirectoryName,
                       Vector fileNames) throws java.io.IOException {
    boolean flag = false;
    try {
      File af = new File(destDirectoryName);
      af.mkdirs();
      for (int i = 0; i < fileNames.size(); i++) {
        cutFile(sourceDirectoryName, (String) fileNames.elementAt(i),
                destDirectoryName, (String) fileNames.elementAt(i));
      }
      /*File af1 = new File(sourceDirectoryName);
             af1.delete();*/
      flag = true;
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return flag;
  }

  /**
   * copyFile(File src,File dest)
   * 把本地一个文件copy到另一个地方
   * @param src File
   * @param dest File
   * @throws IOException
   */
  public static
      void copyFile(File src, File dest) throws java.io.IOException {
    int length = (int) src.length();
    //分块拷贝转发的附件
    int block = 20000000;
    int blocknum = length / block;
    int blockmod = length % block;
    int totalblock = 0;
    if (blockmod == 0) {
      totalblock = blocknum;
    }
    else {
      totalblock = blocknum + 1;
    }
    FileOutputStream outfile = null;
    FileInputStream infile = null;
    try {
      int starthead = 0;
      outfile = new FileOutputStream(dest);
      infile = new FileInputStream(src);
      for (int m = 0; m < totalblock && length != 0; m++) {
        if ( (m + 1) * block > length) {
          byte[] TempAttach = new byte[blockmod];
          infile.read(TempAttach, starthead, blockmod);
          outfile.write(TempAttach, starthead, blockmod);
          outfile.close();
          infile.close();
        }
        else {
          byte[] TempAttach = new byte[block];
          infile.read(TempAttach, starthead, block);
          outfile.write(TempAttach, starthead, block);
        }
      }
    }
    catch (Exception e) {
      System.out.println(e);
    }
    finally {
      outfile.close();
      infile.close();
    }
  }

  /**
   * copyDirectory(String srcDir,String destDir)
   * 把本地一个目录下所有文件copy到另一个目录。
   * @param srcDir String
   * @param destDir String
   */
  public static
      void copyDirectory(String srcDir, String destDir) {
    File srcDirFile = new File(srcDir);
    File destDirFile = new File(destDir);
    if (srcDirFile.exists()) {
      if (destDirFile.exists() == false) {
        destDirFile.mkdir();
      }
      File[] files = srcDirFile.listFiles();
      int i = files.length;
      int a = 0;
      for (a = 0; a < i && i > 0; a++) {
        try {
          copyFile(files[a],
                   new File(destDir
                            + File.separator
                            + files[a].getCanonicalPath().substring(files[a]
              .getCanonicalPath()
              .lastIndexOf(File.separator) + 1)));
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
  }

  /**
   * seperateBasicFilename(String wholeFilename)
   * 从完整的路径名中获取不包含路径信息的文件名
   * @param wholeFilename String
   * @return String
   */
  public static
      String seperateBasicFilename(String wholeFilename) {
    String fg = new String(File.separator);
    int index = wholeFilename.lastIndexOf(fg);
    if (index < 0) {
      return wholeFilename;
    }
    String filename = wholeFilename.substring(index + 1);
    return filename;
  }

  /**
   * seperateSimpleFilename(String wholeFilename)
   * 从完整的路径名中获取不包含路径信息没有扩展名的文件名
   * @param wholeFilename String
   * @return String
   */
  public static
      String seperateSimpleFilename(String wholeFilename) {
    String filename = seperateBasicFilename(wholeFilename);
    String fg = new String(".");
    int index = filename.lastIndexOf(fg);
    if (index < 0) {
      return wholeFilename;
    }
    String simplefilename = filename.substring(0, index);
    return simplefilename;
  }

  /**
   * getFileExt(String fileName)
   * 从文件名中取得文件扩展名
   * @param fileName String
   * @return String
   */
  public static String getFileExt(String fileName) {
    String fileExt = "";

⌨️ 快捷键说明

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