java文件处理工具类详解

本文实例为大家分享了java文件处理工具类的具体代码,供大家参考,具体内容如下

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FileUtil
{
 private static Log logger = LogFactory.getLog(FileUtil.class);

 public static void writeFile(String paramString1, String paramString2)
 {
  writeFile(paramString1, paramString2, "utf-8");
 }

 public static void writeFile(String paramString1, String paramString2, String paramString3)
 {
  try
  {
   createFolder(paramString1, true);
   BufferedWriter localBufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(paramString1), paramString3));
   localBufferedWriter.write(paramString2);
   localBufferedWriter.close();
  }
  catch (IOException localIOException)
  {
   localIOException.printStackTrace();
  }
 }

 public static void writeFile(String paramString, InputStream paramInputStream)
  throws IOException
 {
  FileOutputStream localFileOutputStream = new FileOutputStream(paramString);
  byte[] arrayOfByte = new byte['Ȁ'];
  int i = 0;
  while ((i = paramInputStream.read(arrayOfByte)) != -1) {
   localFileOutputStream.write(arrayOfByte, 0, i);
  }
  paramInputStream.close();
  localFileOutputStream.close();
 }

 public static String readFile(String paramString)
  throws IOException
 {
  try
  {
   File localFile = new File(paramString);
   String str1 = getCharset(localFile);
   StringBuffer localStringBuffer = new StringBuffer();
   BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(paramString), str1));
   String str2;
   while ((str2 = localBufferedReader.readLine()) != null) {
    localStringBuffer.append(str2 + "\r\n");
   }
   localBufferedReader.close();
   return localStringBuffer.toString();
  }
  catch (IOException localIOException)
  {
   throw localIOException;
  }
 }

 public static boolean isExistFile(String paramString)
 {
  boolean bool = false;
  File localFile = new File(paramString);
  if (localFile.isDirectory())
  {
   File[] arrayOfFile = localFile.listFiles();
   if ((arrayOfFile != null) && (arrayOfFile.length != 0)) {
    bool = true;
   }
  }
  return bool;
 }

 public static String getCharset(File paramFile)
 {
  String str = "GBK";
  byte[] arrayOfByte = new byte[3];
  try
  {
   int i = 0;
   BufferedInputStream localBufferedInputStream = new BufferedInputStream(new FileInputStream(paramFile));
   localBufferedInputStream.mark(0);
   int j = localBufferedInputStream.read(arrayOfByte, 0, 3);
   if (j == -1) {
    return str;
   }
   if ((arrayOfByte[0] == -1) && (arrayOfByte[1] == -2))
   {
    str = "UTF-16LE";
    i = 1;
   }
   else if ((arrayOfByte[0] == -2) && (arrayOfByte[1] == -1))
   {
    str = "UTF-16BE";
    i = 1;
   }
   else if ((arrayOfByte[0] == -17) && (arrayOfByte[1] == -69) && (arrayOfByte[2] == -65))
   {
    str = "UTF-8";
    i = 1;
   }
   localBufferedInputStream.reset();
   if (i == 0)
   {
    int k = 0;
    while ((j = localBufferedInputStream.read()) != -1)
    {
     k++;
     if ((j < 240) && ((128 > j) || (j > 191))) {
      if ((192 <= j) && (j <= 223))
      {
       j = localBufferedInputStream.read();
       if (128 > j) {
        break;
       }
       if (j > 191) {
        break;
       }
      }
      else if ((224 <= j) && (j <= 239))
      {
       j = localBufferedInputStream.read();
       if ((128 <= j) && (j <= 191))
       {
        j = localBufferedInputStream.read();
        if ((128 <= j) && (j <= 191)) {
         str = "UTF-8";
        }
       }
      }
     }
    }
   }
   localBufferedInputStream.close();
  }
  catch (Exception localException)
  {
   localException.printStackTrace();
  }
  return str;
 }

 public static byte[] readByte(InputStream paramInputStream)
 {
  try
  {
   byte[] arrayOfByte = new byte[paramInputStream.available()];
   paramInputStream.read(arrayOfByte);
   return arrayOfByte;
  }
  catch (FileNotFoundException localFileNotFoundException)
  {
   logger.error("文件路径不存在:" + localFileNotFoundException.getMessage());
  }
  catch (Exception localException)
  {
   localException.printStackTrace();
  }
  return null;
 }

 public static byte[] readByte(String paramString)
 {
  try
  {
   FileInputStream localFileInputStream = new FileInputStream(paramString);
   byte[] arrayOfByte = new byte[localFileInputStream.available()];
   localFileInputStream.read(arrayOfByte);
   localFileInputStream.close();
   return arrayOfByte;
  }
  catch (FileNotFoundException localFileNotFoundException)
  {
   logger.error("文件路径不存在:" + paramString);
  }
  catch (Exception localException)
  {
   localException.printStackTrace();
  }
  return null;
 }

 public static boolean writeByte(String paramString, byte[] paramArrayOfByte)
 {
  try
  {
   BufferedOutputStream localBufferedOutputStream = new BufferedOutputStream(new FileOutputStream(paramString));
   localBufferedOutputStream.write(paramArrayOfByte);
   localBufferedOutputStream.close();
   return true;
  }
  catch (Exception localException)
  {
   localException.printStackTrace();
  }
  return false;
 }

 public static boolean deleteDir(File paramFile)
 {
  if (paramFile.isDirectory())
  {
   String[] arrayOfString = paramFile.list();
   for (int i = 0; i < arrayOfString.length; i++)
   {
    boolean bool = deleteDir(new File(paramFile, arrayOfString[i]));
    if (!bool) {
     return false;
    }
   }
  }
  return paramFile.delete();
 }

 public static void serializeToFile(Object paramObject, String paramString)
 {
  try
  {
   ObjectOutputStream localObjectOutputStream = new ObjectOutputStream(new FileOutputStream(paramString));
   localObjectOutputStream.writeObject(paramObject);
   localObjectOutputStream.close();
  }
  catch (IOException localIOException)
  {
   localIOException.printStackTrace();
  }
 }

 public static Object deserializeFromFile(String paramString)
 {
  try
  {
   File localFile = new File(paramString);
   ObjectInputStream localObjectInputStream = new ObjectInputStream(new FileInputStream(localFile));
   Object localObject = localObjectInputStream.readObject();
   localObjectInputStream.close();
   return localObject;
  }
  catch (Exception localException)
  {
   localException.printStackTrace();
  }
  return null;
 }

 public static String inputStream2String(InputStream paramInputStream, String paramString)
  throws IOException
 {
  BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(paramInputStream, paramString));
  StringBuffer localStringBuffer = new StringBuffer();
  String str = "";
  while ((str = localBufferedReader.readLine()) != null) {
   localStringBuffer.append(str + "\n");
  }
  return localStringBuffer.toString();
 }

 public static String inputStream2String(InputStream paramInputStream)
  throws IOException
 {
  return inputStream2String(paramInputStream, "utf-8");
 }

 public static File[] getFiles(String paramString)
 {
  File localFile = new File(paramString);
  return localFile.listFiles();
 }

 public static void createFolderFile(String paramString)
 {
  createFolder(paramString, true);
 }

 public static void createFolder(String paramString, boolean paramBoolean)
 {
  if (paramBoolean) {
   paramString = paramString.substring(0, paramString.lastIndexOf(File.separator));
  }
  File localFile = new File(paramString);
  if (!localFile.exists()) {
   localFile.mkdirs();
  }
 }

 public static void createFolder(String paramString1, String paramString2)
 {
  paramString1 = StringUtil.trimSufffix(paramString1, File.separator) + File.separator + paramString2;
  File localFile = new File(paramString1);
  localFile.mkdir();
 }

 public static void renameFolder(String paramString1, String paramString2)
 {
  File localFile = new File(paramString1);
  if (localFile.exists()) {
   localFile.renameTo(new File(paramString2));
  }
 }

 public static ArrayList<File> getDiretoryOnly(File paramFile)
 {
  ArrayList localArrayList = new ArrayList();
  if ((paramFile != null) && (paramFile.exists()) && (paramFile.isDirectory()))
  {
   File[] arrayOfFile = paramFile.listFiles(new FileFilter()
   {
    public boolean accept(File paramAnonymousFile)
    {
     return paramAnonymousFile.isDirectory();
    }
   });
   for (int i = 0; i < arrayOfFile.length; i++) {
    localArrayList.add(arrayOfFile[i]);
   }
  }
  return localArrayList;
 }

 public ArrayList<File> getFileOnly(File paramFile)
 {
  ArrayList localArrayList = new ArrayList();
  File[] arrayOfFile = paramFile.listFiles(new FileFilter()
  {
   public boolean accept(File paramAnonymousFile)
   {
    return paramAnonymousFile.isFile();
   }
  });
  for (int i = 0; i < arrayOfFile.length; i++) {
   localArrayList.add(arrayOfFile[i]);
  }
  return localArrayList;
 }

 public static boolean deleteFile(String paramString)
 {
  File localFile = new File(paramString);
  return localFile.delete();
 }

 public static boolean copyFile(String paramString1, String paramString2)
 {
  File localFile1 = new File(paramString1);
  File localFile2 = new File(paramString2);
  FileInputStream localFileInputStream = null;
  FileOutputStream localFileOutputStream = null;
  try
  {
   localFileInputStream = new FileInputStream(localFile1);
   localFileOutputStream = new FileOutputStream(localFile2);
   byte[] arrayOfByte = new byte['က'];
   int i;
   while ((i = localFileInputStream.read(arrayOfByte)) != -1) {
    localFileOutputStream.write(arrayOfByte, 0, i);
   }
   localFileOutputStream.flush();
   localFileOutputStream.close();
   localFileInputStream.close();
  }
  catch (IOException localIOException)
  {
   localIOException.printStackTrace();
   return false;
  }
  return true;
 }

 public static void backupFile(String paramString)
 {
  String str = paramString + ".bak";
  File localFile = new File(str);
  if (localFile.exists()) {
   localFile.delete();
  }
  copyFile(paramString, str);
 }

 public static String getFileExt(File paramFile)
 {
  if (paramFile.isFile()) {
   return getFileExt(paramFile.getName());
  }
  return "";
 }

 public static String getFileExt(String paramString)
 {
  int i = paramString.lastIndexOf(".");
  if (i > -1) {
   return paramString.substring(i + 1).toLowerCase();
  }
  return "";
 }

 public static void copyDir(String paramString1, String paramString2)
  throws IOException
 {
  new File(paramString2).mkdirs();
  File[] arrayOfFile = new File(paramString1).listFiles();
  for (int i = 0; i < arrayOfFile.length; i++)
  {
   if (arrayOfFile[i].isFile())
   {
    String str1 = arrayOfFile[i].getAbsolutePath();
    String str2 = paramString2 + "/" + arrayOfFile[i].getName();
    copyFile(str1, str2);
   }
   if (arrayOfFile[i].isDirectory()) {
    copyDirectiory(paramString1 + "/" + arrayOfFile[i].getName(), paramString2 + "/" + arrayOfFile[i].getName());
   }
  }
 }

 private static void copyDirectiory(String paramString1, String paramString2)
  throws IOException
 {
  new File(paramString2).mkdirs();
  File[] arrayOfFile = new File(paramString1).listFiles();
  for (int i = 0; i < arrayOfFile.length; i++)
  {
   if (arrayOfFile[i].isFile())
   {
    String str1 = arrayOfFile[i].getAbsolutePath();
    String str2 = paramString2 + "/" + arrayOfFile[i].getName();
    copyFile(str1, str2);
   }
   if (arrayOfFile[i].isDirectory()) {
    copyDirectiory(paramString1 + "/" + arrayOfFile[i].getName(), paramString2 + "/" + arrayOfFile[i].getName());
   }
  }
 }

 public static String getFileSize(File paramFile)
  throws IOException
 {
  if (paramFile.isFile())
  {
   FileInputStream localFileInputStream = new FileInputStream(paramFile);
   int i = localFileInputStream.available();
   localFileInputStream.close();
   return getSize(i);
  }
  return "";
 }

 public static String getSize(double paramDouble)
 {
  DecimalFormat localDecimalFormat = new DecimalFormat("0.00");
  double d;
  if (paramDouble > 1048576.0D)
  {
   d = paramDouble / 1048576.0D;
   return localDecimalFormat.format(d) + " M";
  }
  if (paramDouble > 1024.0D)
  {
   d = paramDouble / 1024.0D;
   return localDecimalFormat.format(d) + " KB";
  }
  return paramDouble + " bytes";
 }

 public static void downLoadFile(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString1, String paramString2)
  throws IOException
 {
  ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
  File localFile = new File(paramString1);
  if (localFile.exists())
  {
   paramHttpServletResponse.setContentType("APPLICATION/OCTET-STREAM");
   String str1 = getFileExt(paramString1);
   if ((!Validation.isEmpty(str1)) && (str1.toLowerCase().equals("apk"))) {
    paramHttpServletResponse.setContentType("application/vnd.android.package-archive");
   }
   String str2 = paramString2;
   String str3 = paramHttpServletRequest.getHeader("USER-AGENT");
   if ((str3 != null) && (str3.indexOf("MSIE") == -1))
   {
    try
    {
     String localObject1 = transCharacter(paramHttpServletRequest, str2);
     paramHttpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + (String)localObject1);
    }
    catch (Exception localException1)
    {
     localException1.printStackTrace();
    }
   }
   else
   {
    str2 = URLEncoder.encode(str2, "utf-8");
    paramHttpServletResponse.addHeader("Content-Disposition", "attachment;filename=" + str2);
   }
   Object localObject1 = null;
   try
   {
    localServletOutputStream = paramHttpServletResponse.getOutputStream();
    localObject1 = new FileInputStream(paramString1);
    byte[] arrayOfByte = new byte['Ѐ'];
    int i = 0;
    while ((i = ((FileInputStream)localObject1).read(arrayOfByte)) > 0) {
     localServletOutputStream.write(arrayOfByte, 0, i);
    }
    localServletOutputStream.flush();
   }
   catch (Exception localException2)
   {
    localException2.printStackTrace();
   }
   finally
   {
    if (localObject1 != null)
    {
     ((FileInputStream)localObject1).close();
     localObject1 = null;
    }
    if (localServletOutputStream != null)
    {
     localServletOutputStream.close();
     localServletOutputStream = null;
     paramHttpServletResponse.flushBuffer();
    }
   }
  }
  else
  {
   localServletOutputStream.write("文件不存在!".getBytes("utf-8"));
  }
 }

 private static String transCharacter(HttpServletRequest paramHttpServletRequest, String paramString)
  throws Exception
 {
  if (paramHttpServletRequest.getHeader("USER-AGENT").toLowerCase().indexOf("msie") > 0) {
   return URLEncoder.encode(paramString, "UTF-8");
  }
  if (paramHttpServletRequest.getHeader("USER-AGENT").toLowerCase().indexOf("firefox") > 0) {
   return new String(paramString.getBytes("UTF-8"), "ISO8859-1");
  }
  return new String(paramString.getBytes("gbk"), "ISO8859-1");
 }

 public static String getParentDir(String paramString1, String paramString2)
 {
  File localFile = new File(paramString2);
  String str1 = localFile.getParent();
  String str2 = str1.replace(paramString1, "");
  return str2.replace(File.separator, "/");
 }

 public static String getClassesPath()
 {
  String str = StringUtil.trimSufffix(AppUtil.getRealPath("/"), File.separator) + "\\WEB-INF\\classes\\".replace("\\", File.separator);
  return str;
 }

 public static String getRootPath()
 {
  String str = StringUtil.trimSufffix(AppUtil.getRealPath("/"), File.separator) + File.separator;
  return str;
 }

 public static String readFromProperties(String paramString1, String paramString2)
 {
  String str1 = "";
  BufferedInputStream localBufferedInputStream = null;
  try
  {
   localBufferedInputStream = new BufferedInputStream(new FileInputStream(paramString1));
   Properties localProperties = new Properties();
   localProperties.load(localBufferedInputStream);
   str1 = localProperties.getProperty(paramString2);
   String str2 = str1;
   return str2;
  }
  catch (Exception localException)
  {
   localException.printStackTrace();
  }
  finally
  {
   if (localBufferedInputStream != null) {
    try
    {
     localBufferedInputStream.close();
    }
    catch (IOException localIOException3)
    {
     localIOException3.printStackTrace();
    }
   }
  }
  return str1;
 }

 public static boolean saveProperties(String paramString1, String paramString2, String paramString3)
 {
  StringBuffer localStringBuffer = new StringBuffer();
  int i = 0;
  BufferedReader localBufferedReader = null;
  try
  {
   localBufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(paramString1), "utf-8"));
   String str;
   while ((str = localBufferedReader.readLine()) != null) {
    if (str.startsWith(paramString2))
    {
     localStringBuffer.append(paramString2 + "=" + paramString3 + "\r\n");
     i = 1;
    }
    else
    {
     localStringBuffer.append(str + "\r\n");
    }
   }
   if (i == 0) {
    localStringBuffer.append(paramString2 + "=" + paramString3 + "\r\n");
   }
   writeFile(paramString1, localStringBuffer.toString(), "utf-8");
   boolean bool1 = true;
   return bool1;
  }
  catch (Exception localException)
  {
   localException.printStackTrace();
   boolean bool2 = false;
   return bool2;
  }
  finally
  {
   if (localBufferedReader != null) {
    try
    {
     localBufferedReader.close();
    }
    catch (IOException localIOException3)
    {
     localIOException3.printStackTrace();
    }
   }
  }
 }

 public static boolean delProperties(String paramString1, String paramString2)
 {
  StringBuffer localStringBuffer = new StringBuffer();
  BufferedReader localBufferedReader = null;
  try
  {
   localBufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(paramString1), "utf-8"));
   String str;
   while ((str = localBufferedReader.readLine()) != null) {
    if (!str.startsWith(paramString2)) {
     localStringBuffer.append(str + "\r\n");
    }
   }
   writeFile(paramString1, localStringBuffer.toString(), "utf-8");
   boolean bool1 = true;
   return bool1;
  }
  catch (Exception localException)
  {
   localException.printStackTrace();
   boolean bool2 = false;
   return bool2;
  }
  finally
  {
   if (localBufferedReader != null) {
    try
    {
     localBufferedReader.close();
    }
    catch (IOException localIOException3)
    {
     localIOException3.printStackTrace();
    }
   }
  }
 }

 public static List<Class<?>> getAllClassesByInterface(Class<?> paramClass, boolean paramBoolean)
  throws IOException, ClassNotFoundException, IllegalStateException
 {
  if (!paramClass.isInterface()) {
   throw new IllegalStateException("Class not a interface.");
  }
  ClassLoader localClassLoader = paramClass.getClassLoader();
  String str = paramBoolean ? paramClass.getPackage().getName() : "/";
  return findClasses(paramClass, localClassLoader, str);
 }

 private static List<Class<?>> findClasses(Class<?> paramClass, ClassLoader paramClassLoader, String paramString)
  throws IOException, ClassNotFoundException
 {
  ArrayList localArrayList = new ArrayList();
  String str = paramString.replace(".", "/");
  Object localObject;
  if (!str.equals("/"))
  {
   localObject = paramClassLoader.getResources(str);
   while (((Enumeration)localObject).hasMoreElements())
   {
    URL localURL = (URL)((Enumeration)localObject).nextElement();
    localArrayList.addAll(findResources(paramClass, new File(localURL.getFile()), paramString));
   }
  }
  else
  {
   localObject = paramClassLoader.getResource("").getPath();
   localArrayList.addAll(findResources(paramClass, new File((String)localObject), paramString));
  }
  return localArrayList;
 }

 private static List<Class<?>> findResources(Class<?> paramClass, File paramFile, String paramString)
  throws ClassNotFoundException
 {
  ArrayList localArrayList = new ArrayList();
  if (!paramFile.exists()) {
   return Collections.EMPTY_LIST;
  }
  File[] arrayOfFile1 = paramFile.listFiles();
  for (File localFile : arrayOfFile1) {
   if (localFile.isDirectory())
   {
    if (!localFile.getName().contains(".")) {
     if (!paramString.equals("/")) {
      localArrayList.addAll(findResources(paramClass, localFile, paramString + "." + localFile.getName()));
     } else {
      localArrayList.addAll(findResources(paramClass, localFile, localFile.getName()));
     }
    }
   }
   else if (localFile.getName().endsWith(".class"))
   {
    Class localClass = null;
    if (!paramString.equals("/")) {
     localClass = Class.forName(paramString + "." + localFile.getName().substring(0, localFile.getName().length() - 6));
    } else {
     localClass = Class.forName(localFile.getName().substring(0, localFile.getName().length() - 6));
    }
    if ((paramClass.isAssignableFrom(localClass)) && (!paramClass.equals(localClass))) {
     localArrayList.add(localClass);
    }
   }
  }
  return localArrayList;
 }

 public static Object cloneObject(Object paramObject)
  throws Exception
 {
  ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
  ObjectOutputStream localObjectOutputStream = new ObjectOutputStream(localByteArrayOutputStream);
  localObjectOutputStream.writeObject(paramObject);
  ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(localByteArrayOutputStream.toByteArray());
  ObjectInputStream localObjectInputStream = new ObjectInputStream(localByteArrayInputStream);
  return localObjectInputStream.readObject();
 }

 public static boolean isFileType(String paramString1, String paramString2)
 {
  boolean bool = false;
  if (("IMAGE".equals(paramString2)) && ((paramString1.toUpperCase().equals("JPG")) || (paramString1.toUpperCase().equals("PNG")) || (paramString1.toUpperCase().equals("GIF")) || (paramString1.toUpperCase().equals("JPEG")))) {
   bool = true;
  }
  return bool;
 }

 public static boolean isFileType(String paramString, String[] paramArrayOfString)
 {
  boolean bool = false;
  if ((paramArrayOfString != null) && (paramArrayOfString.length > 0)) {
   for (int i = 0; i < paramArrayOfString.length; i++) {
    if (paramString.toUpperCase().equals(paramArrayOfString[i].toUpperCase())) {
     return true;
    }
   }
  }
  return bool;
 }

 public static boolean isErrorFileType(String paramString)
 {
  String[] arrayOfString = null;
  String str = AppConfigUtil.get("File_Filter_Val");
  if ((AppConfigUtil.getICache("FILEFILTER_KEY") == null) && (str != null))
  {
   arrayOfString = str.split("\\|");
   AppConfigUtil.setICache("FILEFILTER_KEY", arrayOfString);
  }
  else
  {
   arrayOfString = (String[])AppConfigUtil.getICache("FILEFILTER_KEY");
  }
  return isFileType(paramString, arrayOfString);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • java工具类之实现java获取文件行数

    工具类代码,取得当前项目中所有java文件总行数,代码行数,注释行数,空白行数 复制代码 代码如下: import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.Reader;import

  • Java文件操作工具类fileUtil实例【文件增删改,复制等】

    本文实例讲述了Java文件操作工具类fileUtil.分享给大家供大家参考,具体如下: package com.gcloud.common; import java.io.*; import java.net.MalformedURLException; import java.net.URL; /** * 文件工具类 * Created by charlin on 2017/9/8. */ public class FileUtil { /** * 读取文件内容 * * @param is *

  • java文件操作工具类实现复制文件和文件合并

    两个方法:1.复制一个目录下面的所有文件和文件夹2.将一个文件目录下面的所有文本文件合并到同一个文件中 复制代码 代码如下: package com.firewolf.test; import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException; public class FileReaderUtil { public static void

  • 一个Java配置文件加密解密工具类分享

    常见的如: 数据库用户密码,短信平台用户密码,系统间校验的固定密码等.本工具类参考了 <Spring.3.x企业应用开发实战>一书 5.3节的实现.完整代码与注释信息如下: 复制代码 代码如下: package com.cncounter.util.comm; import java.security.Key;import java.security.SecureRandom; import javax.crypto.Cipher;import javax.crypto.KeyGenerato

  • java文件操作工具类

    最近为了修改大量收藏的美剧文件名,用swing写了个小工具,代码是文件处理部分,具体内容如下 package datei.steuern; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter

  • java文件操作工具类分享(file文件工具类)

    复制代码 代码如下: import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.Fil

  • java文件读写工具类分享

    本文实例为大家分享了java文件读写工具类的具体代码,供大家参考,具体内容如下 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.FileWriter;

  • Java实现的获取和判断文件头信息工具类用法示例

    本文实例讲述了Java实现的获取和判断文件头信息工具类用法.分享给大家供大家参考,具体如下: package test; import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; /** * 获取和判断文件头信息 * * @author Sud * */ public class GetTypeByHead { // 缓存文件头信息-文件头信息 public static final

  • Java实现的文件上传下载工具类完整实例【上传文件自动命名】

    本文实例讲述了Java实现的文件上传下载工具类.分享给大家供大家参考,具体如下: 这是一个在Eclipse环境下采用Java语言实现文件上传下载的工具类.和之前介绍的C#文件上传下载工具类一样,在上传时,为避免文件名在服务器中重复,采用"服务器时间(定义到毫秒)+文件名+文件后缀"的方式作为服务器上的文件名:下载过程中利用 spring mvc ResponseEntity 做文件下载,返回的是字节流,下载成功后可自定义文件的保存路径. 具体源码如下所示: package com.ut

  • java文件处理工具类详解

    本文实例为大家分享了java文件处理工具类的具体代码,供大家参考,具体内容如下 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream;

  • Java项目常见工具类详解

    目录 JWT工具类 MD5工具类 视频点播工具类 公共常量工具类 日期操作工具类 Http客户端工具类 获取IP工具类 JWT工具类 这里一共涉及四个方法: 传入用户信息获得token 传入token字符串判断token是否存在与有效 传入HttpServletRequest,通过获取Header中的token判断是否存在与有效 根据token获取用户id public class JwtUtils { //token过期时间 public static final long EXPIRE =

  • Java 基础--Arrays工具类详解

    目录 1.数组转换成字符串 2.数组转换成集合 (1)asList (2)stream 流操作 (3)Collections.addAll() 3.对数组进行升序排列 4.判断数组是否相等 5.所有元素赋特定值 6.对数组复制 7.查询数组下标 8.返回数组 hashcode 值 总结 Arrays 是 JDK 提供的操作数组的工具类,Arrays 类提供了动态创建.访问和操作 Java 数组的方法.此类还包含一个允许将数组作为列表来查看的静态工厂. 1.数组转换成字符串 public stat

  • Java TimedCache 带时间缓存工具类详解使用

    简述 我们在工作中会碰到需要使用带过期时间的缓存场景.但是使用redis有太重了,毕竟缓存的数据很小,放在内存够够的.hutools提供了TimedCache时间缓存工具,可以实现该场景.下面使用到该组件,并为了适配工作场景,对该工具类做优化升级. Maven依赖 <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>

  • Springboot Mybatis Plus自动生成工具类详解代码

    前言 代码生成器,也叫逆向工程,是根据数据库里的表结构,自动生成对应的实体类.映射文件和接口. 看到很多小伙伴在为数据库生成实体类发愁,现分享给大家,提高开发效率. 一.pom依赖 <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> &

  • JAVA中的Configuration类详解

    本文主要研究的是Java中的Configuration类的用法,涉及maven自动加载,pom.xml配置和简单的Java代码,具体如下. properties文件是Java平台默认的配置文件格式,其优点是格式清晰,简单易懂,使用commons-configuration读取properties文件也比较简单,代码如下: 基本用法: 1.加载jar包,我使用maven自动加载,pom.xml配置如下: <dependency> <groupId>commons-configurat

  • Java并发编程ThreadLocalRandom类详解

    目录 为什么需要ThreadLocalRandom ThreadRandom原理详解 为什么需要ThreadLocalRandom java.util.Random一直都是使用比较广泛的随机数生成工具类,而且java.lang.Math中的随机数生成也是使用的java.util.Random实例. 我们下面看一下java.util.Random的使用方法: import java.util.Random; public class code_4_threadRandom { public sta

  • 基于Java中的StringTokenizer类详解(推荐)

    StringTokenizer是字符串分隔解析类型,属于:Java.util包. 1.StringTokenizer的构造函数 StringTokenizer(String str):构造一个用来解析str的StringTokenizer对象.java默认的分隔符是"空格"."制表符('\t')"."换行符('\n')"."回车符('\r')". StringTokenizer(String str,String delim)

  • 微信小程序 常用工具类详解及实例

    微信小程序 常用工具类详解 前言: 做微信小程序当中,会遇到好多的工具类util.js,这里记载下来以便平常使用 (Ps:建议通过目录查看) -获取日期(格式化) function formatTime(date) { var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() var hour = date.getHours() var minute = date.getMinut

随机推荐