日期:2014-05-16  浏览次数:20833 次

Java File工具类附带MYSQL SQL转HSQL SQL字符串处理
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Random;
import java.util.Vector;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * 
 * 文件操作的工具类
 * 
 * @version [8.0,2011-5-20 下午04:29:53]
 */
public class FileUtil {

    private static final int DEFAULT_READING_SIZE = 8192;

    public static void copyFile(File sourceFile, File destFile) throws IOException {
        if (!destFile.exists()) {
            File parent = destFile.getParentFile();
            if (parent != null) {
                if (!parent.exists()) {
                    parent.mkdirs();
                }
            }
            destFile.createNewFile();
        }
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(sourceFile);
            out = new FileOutputStream(destFile);
            copy(in, out);
        } finally {
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        }
    }

    public static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = in.read(buf, 0, buf.length)) != -1) {
            out.write(buf, 0, len);
        }
    }

    public static void writeFile(File file, byte[] contents) throws IOException {
        if (!file.exists()) {
            file.createNewFile();
        }
        if (!file.isFile()) {
            throw new IOException("File to be written not exist, file path : " + file.getAbsolutePath());
        }
        FileOutputStream fileOut = null;
        BufferedOutputStream bOutput = null;
        try {
            fileOut = new FileOutputStream(file);
            bOutput = new BufferedOutputStream(fileOut);
            bOutput.write(contents);
            bOutput.flush();
        } finally {
            if (fileOut != null) {
                try {
                    fileOut.close();
                } catch (IOException e) {
                }
            }
            if (bOutput != null) {
                try {
                    bOutput.close();
                } catch (IOException e) {
                }
            }
        }

    }

    public static byte[] readFileContent(File file) throws IOException {
        if (!file.exists() || !file.isFile()) {
            throw new IOException("File to be readed not exist, file path : " + file.getAbsolutePath());
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        FileInputStream in = new FileInputStream(file);
        byte[] contents = null;
        try {
            copy(in, out);
            contents = out.toByteArray();
        } catch (IOException e) {
            throw e;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                };
            }

        }
        return contents;
    }

    public static String readFileContentAsString(File file) throws IOException {
        return readFileContentAsString(file, null);
    }

    public static String readFileConten