All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tukeof.common.util.FileUtil Maven / Gradle / Ivy

The newest version!
package com.tukeof.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.URLConnection;
import java.nio.charset.Charset;

public final class FileUtil {

    // recurse to mkdir parent diretory
    public static void recurseMkDir(String file) {
        recurseMkDir(new File(file));
    }

    public static void recurseMkDir(File file) {
        File parent = file.getParentFile();
        if (!parent.getParentFile().exists()) {
            recurseMkDir(parent);
        }

        parent.mkdir();
    }

    // ==== ==== ==== ====    ==== ==== ==== ====    ==== ==== ==== ====

    public static String mimeName(String filename) {
        return URLConnection.getFileNameMap().getContentTypeFor(filename);
    }

    public static byte[] bytes(String filename) throws IOException {
        return bytes(new File(filename));
    }

    public static byte[] bytes(String filename, int len) throws IOException {
        return bytes(new File(filename), len);
    }

    public static byte[] bytes(File file) throws IOException {
        return bytes(file, (int) file.length());
    }

    public static byte[] bytes(File file, int len) throws IOException {
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] bytes = new byte[len];
            IOUtil.readFully(fis, bytes);
            return bytes;
        }
    }

    public static char[] chars(String filename) throws IOException {
        return chars(new File(filename));
    }

    public static char[] chars(String filename, int len) throws IOException {
        return chars(new File(filename), len);
    }

    public static char[] chars(File file) throws IOException {
        return chars(file, (int) file.length());
    }

    public static char[] chars(File file, int len) throws IOException {
        try (FileReader f = new FileReader(file)) {
            char[] chars = new char[len];
            IOUtil.readFully(f, chars);
            return chars;
        }
    }

    public static String string(String filename) throws IOException {
        return string(new File(filename));
    }

    public static String string(File file) throws IOException {
        return new String(chars(file));
    }

    // ==== ==== ==== ====    ==== ==== ==== ====    ==== ==== ==== ====

    public static void convert(File srcFile, Charset srcCS, File destFile, Charset destCS)
            throws IOException {
        if (srcFile.isFile()) {
            convertFile(srcFile, srcFile, srcCS, destFile, destCS);
        } else {
            File[] files = srcFile.listFiles();
            if (files == null || files.length < 1) return;

            for (File file : files) {
                convertFile(file, srcFile, srcCS, destFile, destCS);
            }
        }
    }

    // ==== ==== ==== ====    ==== ==== ==== ====    ==== ==== ==== ====

    private static void convertFile(File file, File srcFile, Charset srcCS, File destFile, Charset destCS) throws IOException {
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(file), srcCS))) {
            String path = destFile.getPath()
                    + file.getAbsolutePath().substring(srcFile.getPath().length());
            File outfile = new File(path);
            recurseMkDir(outfile);
            try (BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(outfile), destCS))) {
                for (String line = reader.readLine(); line != null; line = reader.readLine()) {
                    writer.write(line);
                    writer.write(File.separator);
                }
                writer.flush();
            }
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy