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

io.github.linmoure.thread.utils.FileUtils Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package io.github.linmoure.thread.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.*;

@Slf4j
public final class FileUtils {

    public static String readConfig(String filePath) {
        File resourceFile = new File(filePath);
        return resourceFile.exists() ? readFile(filePath) : readResource(filePath);
    }

    public static String readFile(String filePath) {
        File resultFile = new File(filePath);
        StringBuilder sb = new StringBuilder(2048);

        try {
            RandomAccessFile file = new RandomAccessFile(resultFile, "r");
            Throwable var4 = null;

            try {
                for (String line = file.readLine(); StringUtils.isNotBlank(line); line = file.readLine()) {
                    sb.append(line);
                }
            } catch (Throwable var14) {
                var4 = var14;
                throw var14;
            } finally {
                if (file != null) {
                    if (var4 != null) {
                        try {
                            file.close();
                        } catch (Throwable var13) {
                            var4.addSuppressed(var13);
                        }
                    } else {
                        file.close();
                    }
                }

            }
        } catch (IOException var16) {
            log.error(var16.getMessage(), var16);
        }

        return sb.toString();
    }

    public static String readResource(String resourceName) {
        String content = "";

        try {
            Reader r = new InputStreamReader(Thread.currentThread().getContextClassLoader().getResource(resourceName).openStream());
            Throwable var3 = null;

            try {
                char[] buffer = new char[8196];
                StringBuilder sb = new StringBuilder(8196);
                boolean var6 = true;

                int flag;
                while ((flag = r.read(buffer)) != -1) {
                    sb.append(buffer, 0, flag);
                }

                content = sb.toString();
            } catch (Throwable var15) {
                var3 = var15;
                throw var15;
            } finally {
                if (r != null) {
                    if (var3 != null) {
                        try {
                            r.close();
                        } catch (Throwable var14) {
                            var3.addSuppressed(var14);
                        }
                    } else {
                        r.close();
                    }
                }

            }
        } catch (IOException var17) {
            log.error(var17.getMessage(), var17);
        }

        return content;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy