首页

关于enough-webprocessor源码包FileUtil工具类对文件复制、写入等操作

标签:FileUtil,文件工具类,enough,webprocessor     发布时间:2018-03-16   

一、前言

关于enough-webprocessor.jar源码包中的de.enough.webprocessor.util.FileUtil包工具类,对文件读取字符串数组readTextFile、写出数据到文件writeTextFile、文件复制迁移copy等操作。

二、源码说明

package de.enough.webprocessor.util;@b@@b@import java.io.BufferedReader;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileNotFoundException;@b@import java.io.FileOutputStream;@b@import java.io.FileReader;@b@import java.io.FileWriter;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.io.PrintWriter;@b@import java.util.ArrayList;@b@@b@public final class FileUtil@b@{@b@  public static String[] readTextFile(String fileName)@b@    throws FileNotFoundException, IOException@b@  {@b@    return readTextFile(new File(fileName));@b@  }@b@@b@  public static String[] readTextFile(File file)@b@    throws FileNotFoundException, IOException@b@  {@b@    String line;@b@    ArrayList lines = new ArrayList();@b@    BufferedReader in = new BufferedReader(new FileReader(file));@b@@b@    while ((line = in.readLine()) != null)@b@      lines.add(line);@b@@b@    in.close();@b@    return ((String[])lines.toArray(new String[lines.size()]));@b@  }@b@@b@  public static void writeTextFile(File file, String[] lines)@b@    throws IOException@b@  {@b@    File parentDir = file.getParentFile();@b@    if (!(parentDir.exists()))@b@      parentDir.mkdirs();@b@@b@    PrintWriter out = new PrintWriter(new FileWriter(file));@b@    for (int i = 0; i < lines.length; ++i)@b@      out.println(lines[i]);@b@@b@    out.close();@b@  }@b@@b@  public static void copy(File[] files, File targetDir)@b@    throws FileNotFoundException, IOException@b@  {@b@    String targetPath = targetDir.getAbsolutePath() + File.separatorChar;@b@    byte[] buffer = new byte[1048576];@b@    for (int i = 0; i < files.length; ++i) {@b@      File file = files[i];@b@      copy(file, new File(targetPath + file.getName()), buffer);@b@    }@b@  }@b@@b@  public static void copy(File source, File target)@b@    throws FileNotFoundException, IOException@b@  {@b@    copy(source, target, new byte[1048576]);@b@  }@b@@b@  private static void copy(File source, File target, byte[] buffer)@b@    throws FileNotFoundException, IOException@b@  {@b@    int read;@b@    InputStream in = new FileInputStream(source);@b@@b@    File parentDir = target.getParentFile();@b@    if (!(parentDir.exists()))@b@      parentDir.mkdirs();@b@@b@    OutputStream out = new FileOutputStream(target);@b@    try@b@    {@b@      while ((read = in.read(buffer)) != -1)@b@        out.write(buffer, 0, read);@b@    }@b@    catch (IOException e) {@b@      throw e;@b@    } finally {@b@      in.close();@b@      out.close();@b@    }@b@  }@b@}