首页

关于oschrenk-util源码包IOUtils输入输出工具类对文件常见移动复制等操作源码说明

标签:oschrenk-util,IOUtils,输入输出工具类,java-util     发布时间:2018-07-22   

一、前言

关于oschrenk-util源码包中com.oschrenk.utils.IOUtils输入输出工具类,进行文件移动move、指定路径读取readTextFile为字符串/读取为二进制数组readBinary/、写出文本字符串内容指定路径writeTextFile及目录的常见操作,详情参见源码说明。

二、源码说明 

package com.oschrenk.utils;@b@@b@import com.google.common.base.Nullable;@b@import java.io.BufferedInputStream;@b@import java.io.BufferedOutputStream;@b@import java.io.BufferedReader;@b@import java.io.BufferedWriter;@b@import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.Closeable;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileOutputStream;@b@import java.io.Flushable;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.InputStreamReader;@b@import java.io.OutputStream;@b@import java.io.OutputStreamWriter;@b@import java.io.PrintStream;@b@import java.io.PrintWriter;@b@import java.io.UnsupportedEncodingException;@b@import java.util.Set;@b@@b@public class IOUtils@b@{@b@  private static final String COPY_PREFIX = "copy";@b@@b@  public static boolean move(File from, File to)@b@  {@b@    return from.renameTo(to);@b@  }@b@@b@  public static boolean move(File from, File toDir, String newFilename)@b@  {@b@    return from.renameTo(new File(toDir, newFilename));@b@  }@b@@b@  public static boolean move(File from, File toDir, File toFile)@b@  {@b@    return from.renameTo(new File(toDir, toFile.getName()));@b@  }@b@@b@  public static String readTextFile(String path)@b@    throws IOException@b@  {@b@    return readTextFile(new File(path));@b@  }@b@@b@  public static String readTextFile(File file)@b@    throws IOException@b@  {@b@    return readTextFile(file, getDefaultInputEncoding());@b@  }@b@@b@  public static String readTextFile(String path, String encoding)@b@    throws IOException, UnsupportedEncodingException@b@  {@b@    return readTextFile(new File(path), encoding);@b@  }@b@@b@  public static String readTextFile(File file, String encoding)@b@    throws IOException, UnsupportedEncodingException@b@  {@b@    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));@b@@b@    StringBuffer stringBuffer = new StringBuffer();@b@    try@b@    {@b@      while ((line = bufferedReader.readLine()) != null) {@b@        String line;@b@        stringBuffer.append(line);@b@        stringBuffer.append(System.getProperty("line.separator"));@b@      }@b@    } finally {@b@      bufferedReader.close();@b@    }@b@@b@    return stringBuffer.toString();@b@  }@b@@b@  public static byte[] readBinary(String path)@b@    throws IOException@b@  {@b@    return readBinary(new File(path));@b@  }@b@@b@  public static byte[] readBinary(File file)@b@    throws IOException@b@  {@b@    BufferedInputStream bis = null;@b@    try {@b@      bis = new BufferedInputStream(new FileInputStream(file));@b@      int size = bis.available();@b@      byte[] bytes = new byte[size];@b@@b@      bis.read(bytes);@b@      byte[] arrayOfByte1 = bytes;@b@@b@      return arrayOfByte1; } finally { close(new Closeable[] { bis });@b@    }@b@  }@b@@b@  public static void writeBinaryFile(byte[] data, String path)@b@    throws IOException@b@  {@b@    writeBinaryFile(data, new File(path));@b@  }@b@@b@  public static void writeBinaryFile(byte[] data, File file)@b@    throws IOException@b@  {@b@    InputStream in = new BufferedInputStream(new ByteArrayInputStream(data));@b@@b@    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));@b@@b@    byte[] buffer = new byte[65536];@b@    int read = -1;@b@    try {@b@      while ((read = in.read(buffer)) >= 0)@b@        out.write(buffer, 0, read);@b@    }@b@    finally {@b@      in.close();@b@      out.close();@b@    }@b@  }@b@@b@  public static void writeTextFile(String text, String path)@b@    throws IOException@b@  {@b@    writeTextFile(text, new File(path));@b@  }@b@@b@  public static void writeTextFile(String text, File file)@b@    throws IOException@b@  {@b@    writeTextFile(text, getDefaultOutputEncoding(), file);@b@  }@b@@b@  public static void writeTextFile(String text, String encoding, String path)@b@    throws IOException@b@  {@b@    writeTextFile(text, encoding, new File(path));@b@  }@b@@b@  public static void writeTextFile(String text, String encoding, File file)@b@    throws IOException@b@  {@b@    try@b@    {@b@      file.delete();@b@    } catch (SecurityException se) {@b@      throw new IOException("Can't delete old file. Check Access Control.");@b@    }@b@@b@    appendText(text, encoding, file);@b@  }@b@@b@  public static void appendText(String text, String path)@b@    throws IOException@b@  {@b@    appendText(text, new File(path));@b@  }@b@@b@  public static void appendText(String text, File file)@b@    throws IOException@b@  {@b@    appendText(text, getDefaultOutputEncoding(), file);@b@  }@b@@b@  public static void appendText(String text, String encoding, String path)@b@    throws IOException@b@  {@b@    appendText(text, encoding, new File(path));@b@  }@b@@b@  public static void appendText(String text, String encoding, File file)@b@    throws IOException@b@  {@b@    if (!(file.exists())) {@b@      try {@b@        file.createNewFile();@b@      } catch (IOException e) {@b@        throw new IOException("Can't create new File. Check Access Control.");@b@      }@b@@b@    }@b@@b@    if (!(file.canWrite())) {@b@      throw new IOException("Can't write to file. Check Access Control.");@b@    }@b@@b@    PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), encoding)));@b@    try@b@    {@b@      pw.println();@b@      pw.print(text);@b@    } finally {@b@      pw.close();@b@    }@b@  }@b@@b@  public static String getExtension(String path)@b@  {@b@    int lastSep = path.lastIndexOf(Systems.FILE_SEPARATOR);@b@    int lastExt = path.lastIndexOf(46);@b@@b@    if ((lastExt == -1) || (lastSep > lastExt))@b@      return "";@b@@b@    return path.substring(lastExt + 1, path.length());@b@  }@b@@b@  public static String getExtension(File file)@b@  {@b@    return getExtension(file.getName());@b@  }@b@@b@  public static String getFilename(String path)@b@  {@b@    String name = path.substring(path.lastIndexOf(Systems.FILE_SEPARATOR) + 1, path.length());@b@@b@    if (name.lastIndexOf(46) >= 0) {@b@      return name.substring(0, name.lastIndexOf(46));@b@    }@b@@b@    return name;@b@  }@b@@b@  public static String getFilename(File file)@b@  {@b@    return getFilename(file.getName());@b@  }@b@@b@  public static String getBaseDir(File file)@b@    throws IOException@b@  {@b@    String path = file.getCanonicalPath();@b@    return path.substring(0, path.lastIndexOf(Systems.FILE_SEPARATOR));@b@  }@b@@b@  public static String getBaseDir(String path)@b@  {@b@    return path.substring(0, path.lastIndexOf(Systems.FILE_SEPARATOR));@b@  }@b@@b@  public static String getDefaultOutputEncoding()@b@  {@b@    return new OutputStreamWriter(new ByteArrayOutputStream(0)).getEncoding();@b@  }@b@@b@  public static String getDefaultInputEncoding()@b@  {@b@    return new InputStreamReader(new ByteArrayInputStream(new byte[0])).getEncoding();@b@  }@b@@b@  public static boolean makeDirectories(Set<File> directories)@b@    throws IOException@b@  {@b@    boolean result = true;@b@    for (File file : directories)@b@      result = (result) && (file.mkdirs());@b@@b@    return result;@b@  }@b@@b@  public static boolean deleteDirectory(File dir)@b@  {@b@    String[] children;@b@    int i;@b@    if (dir.isDirectory()) {@b@      children = dir.list();@b@      for (i = 0; i < children.length; ++i) {@b@        boolean success = deleteDirectory(new File(dir, children[i]));@b@        if (!(success))@b@          return false;@b@@b@      }@b@@b@    }@b@@b@    return dir.delete();@b@  }@b@@b@  public static File getAutoRename(File file)@b@    throws IOException@b@  {@b@    if (!(file.exists())) {@b@      return file;@b@    }@b@@b@    if (file.isDirectory()) {@b@      throw new IllegalArgumentException("Can't rename directories.");@b@    }@b@@b@    String filename = getFilename(file.getName());@b@@b@    StringBuilder sb = new StringBuilder();@b@    sb.append(getBaseDir(file));@b@    sb.append(filename);@b@@b@    int pos = filename.lastIndexOf("copy");@b@@b@    if (pos < 0) {@b@      sb.append(" copy");@b@      sb.append(getExtension(file.getName()));@b@      return getAutoRename(new File(sb.toString()));@b@    }@b@@b@    if (pos < filename.length() - "copy".length()) {@b@      int number;@b@      try {@b@        number = Integer.parseInt(filename.substring(pos, filename.length()));@b@      }@b@      catch (NumberFormatException e)@b@      {@b@        sb.append(" copy");@b@        sb.append(getExtension(file.getName()));@b@        return getAutoRename(new File(sb.toString()));@b@      }@b@@b@      sb.append(" ");@b@      sb.append(number + 1);@b@      sb.append(getExtension(file.getName()));@b@      return getAutoRename(new File(sb.toString()));@b@    }@b@@b@    return file;@b@  }@b@@b@  public static String streamToString(InputStream stream)@b@    throws IOException@b@  {@b@    InputStreamReader reader;@b@    try@b@    {@b@      reader = new InputStreamReader(stream);@b@      BufferedReader buffered = new BufferedReader(reader);@b@      StringBuilder builder = new StringBuilder();@b@@b@      while ((line = buffered.readLine()) != null) {@b@        String line;@b@        builder.append(line);@b@        builder.append("\n");@b@      }@b@      String str1 = builder.toString();@b@@b@      return str1; } finally { close(new Closeable[] { stream });@b@    }@b@  }@b@@b@  public static void close(@Nullable Closeable[] inputs)@b@  {@b@    if (inputs == null)@b@      return;@b@    Closeable[] arr$ = inputs; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { Closeable input = arr$[i$];@b@      if (input instanceof Flushable)@b@        try {@b@          ((Flushable)input).flush();@b@        } catch (IOException e) {@b@          System.err.println("IO Exception during closing, when trying to flush stream (" + input + ")." + e);@b@        }@b@      try {@b@        input.close();@b@      } catch (IOException e) {@b@        System.err.println("IO Exception during closing stream (" + input + ")." + e);@b@      }@b@    }@b@  }