Java文件操作工具类

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;

public class FileUtils {

    /**
     * 从输入流中读取string
     * @param inputStream
     * @return
     */
    public static String getString(InputStream inputStream){
        String returnStr = "";
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            StringBuffer sb = new StringBuffer();
            String str = "";
            while ((str = reader.readLine()) != null)
            {
                sb.append(str).append("\n");
            }
            returnStr = sb.toString();
        }catch(Exception e){

        }
        return returnStr;
    }


    /**
     * 新建一个文件并写入内容
     *
     * @param String path     文件全路径
     * @param String fileName 文件名
     * @param String content  内容
     * @param int bufLen      设置缓冲区大小
     * @param boolean isWrite 是否追加写入文件
     * @return boolean
     * @throws IOException
     */
    public  static boolean newStaticFile(String path, String fileName, String content,
                                         int bufLen, boolean isWrite)  {

        if (path == null || path.equals("") || content == null
                || content.equals(""))
            return false;
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        FileOutputStream fos = null;
        Writer out = null;
        try {
            fos = new FileOutputStream(path + File.separator + fileName,
                    isWrite);
            out = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"),
                    bufLen);
            out.write(content);
            out.flush();
            flag = true;
            //log.info(content);
        } catch (IOException e) {
            System.out.println("写入文件出错");
            flag = false;
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }


    /**
     * 复制一个目录及其子目录、文件到另外一个目录
     * @param src
     * @param dest
     * @throws IOException
     */
    public static void copyFolder(File src, File dest){
        try{
            if (src.isDirectory()) {
                if (!dest.exists()) {
                    dest.mkdirs();
                }
                String files[] = src.list();
                for (String file : files) {
                    File srcFile = new File(src, file);
                    File destFile = new File(dest, file);
                    // 递归复制
                    copyFolder(srcFile, destFile);
                }
            } else {
                InputStream in = new FileInputStream(src);
                OutputStream out = new FileOutputStream(dest);

                byte[] buffer = new byte[1024];

                int length;

                while ((length = in.read(buffer)) > 0) {
                    out.write(buffer, 0, length);
                }
                in.close();
                out.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 递归删除目录下的所有文件及子目录下所有文件
     * @param dir 将要删除的文件目录
     * @return boolean Returns "true" if all deletions were successful.
     *                 If a deletion fails, the method stops attempting to
     *                 delete and returns "false".
     */
    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i=0; i

你可能感兴趣的:(Java开发)