properties和json格式文件解析

properties文件解析

public static void loadProps(String propertiesUrl) {
    props = new Properties();
    InputStream in = null;
    try {
        in =new BufferedInputStream(new FileInputStream(propertiesUrl));
        props.load(in);
    } catch (FileNotFoundException e) {
        logger.error("jdbc.properties⽂件未找到");
    } catch (IOException e) {
        logger.error("出现IOException");
    } finally {
        try {
            if (null != in) {
                in.close();
            }
        } catch (IOException e) {
            logger.error("jdbc.properties⽂件流关闭出现异常");
        }
    }
    logger.debug("加载properties⽂件内容完成...........");
    logger.debug("properties⽂件内容:" + props);
}
public static String getProperty(String key) {
if (null == props) {
logger.debug("加载配置⽂件出错");
}
return props.getProperty(key);
}

json文件解析

public static void loadJSON(String jsonUrl) {
        InputStream in = null;
        try {
            in = new FileInputStream(jsonUrl);
            List<CmDynamicModelReflect> allDynamic = JSON.parseArray(IoUtil.read(in, "utf-8"), CmDynamicModelReflect.class);
            if(allDynamic!=null&&allDynamic.size()>0){
                for(CmDynamicModelReflect dynamicBean:allDynamic){
                    dynamicModelReflect.put(dynamicBean.getFileKeyWord(),dynamicBean.getModelName());
                }
            }
        } catch (FileNotFoundException e) {
            logger.error("cm_dynamic_model_reflect⽂件未找到");
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {
                logger.error("cm_dynamic_model_reflect⽂件流关闭出现异常");
            }
        }
        logger.debug("cm_dynamic_model_reflect⽂件内容完成...........");
        System.out.println("cm_dynamic_model_reflect⽂件内容:" + props);
    }
iotUtil工具类
import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PushbackInputStream;
import java.io.PushbackReader;
import java.io.Reader;
import java.io.Serializable;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Objects;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.Checksum;

public class IoUtil {
    public static final int DEFAULT_BUFFER_SIZE = 8192;
    public static final int DEFAULT_MIDDLE_BUFFER_SIZE = 16384;
    public static final int DEFAULT_LARGE_BUFFER_SIZE = 32768;
    public static final int EOF = -1;

    public IoUtil() {
    }

    public static long copy(Reader reader, Writer writer) throws IORuntimeException {
        return copy((Reader)reader, (Writer)writer, 8192);
    }

    public static long copy(Reader reader, Writer writer, int bufferSize) throws IORuntimeException {
        return copy((Reader)reader, (Writer)writer, bufferSize, (StreamProgress)null);
    }

    public static long copy(Reader reader, Writer writer, int bufferSize, StreamProgress streamProgress) throws IORuntimeException {
        char[] buffer = new char[bufferSize];
        long size = 0L;
        if (null != streamProgress) {
            streamProgress.start();
        }

        int readSize;
        try {
            while((readSize = reader.read(buffer, 0, bufferSize)) != -1) {
                writer.write(buffer, 0, readSize);
                size += (long)readSize;
                writer.flush();
                if (null != streamProgress) {
                    streamProgress.progress(size);
                }
            }
        } catch (Exception var9) {
            throw new IORuntimeException(var9);
        }

        if (null != streamProgress) {
            streamProgress.finish();
        }

        return size;
    }

    public static long copy(InputStream in, OutputStream out) throws IORuntimeException {
        return copy((InputStream)in, (OutputStream)out, 8192);
    }

    public static long copy(InputStream in, OutputStream out, int bufferSize) throws IORuntimeException {
        return copy((InputStream)in, (OutputStream)out, bufferSize, (StreamProgress)null);
    }

    public static long copy(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) throws IORuntimeException {
        Assert.notNull(in, "InputStream is null !", new Object[0]);
        Assert.notNull(out, "OutputStream is null !", new Object[0]);
        if (bufferSize <= 0) {
            bufferSize = 8192;
        }

        byte[] buffer = new byte[bufferSize];
        if (null != streamProgress) {
            streamProgress.start();
        }

        long size = 0L;

        int readSize;
        try {
            while((readSize = in.read(buffer)) != -1) {
                out.write(buffer, 0, readSize);
                size += (long)readSize;
                out.flush();
                if (null != streamProgress) {
                    streamProgress.progress(size);
                }
            }
        } catch (IOException var8) {
            throw new IORuntimeException(var8);
        }

        if (null != streamProgress) {
            streamProgress.finish();
        }

        return size;
    }

    public static long copyByNIO(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) throws IORuntimeException {
        return copy(Channels.newChannel(in), Channels.newChannel(out), bufferSize, streamProgress);
    }

    public static long copy(FileInputStream in, FileOutputStream out) throws IORuntimeException {
        Assert.notNull(in, "FileInputStream is null!", new Object[0]);
        Assert.notNull(out, "FileOutputStream is null!", new Object[0]);
        FileChannel inChannel = null;
        FileChannel outChannel = null;

        long var4;
        try {
            inChannel = in.getChannel();
            outChannel = out.getChannel();
            var4 = inChannel.transferTo(0L, inChannel.size(), outChannel);
        } catch (IOException var9) {
            throw new IORuntimeException(var9);
        } finally {
            close((Closeable)outChannel);
            close((Closeable)inChannel);
        }

        return var4;
    }

    public static long copy(ReadableByteChannel in, WritableByteChannel out) throws IORuntimeException {
        return copy((ReadableByteChannel)in, (WritableByteChannel)out, 8192);
    }

    public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize) throws IORuntimeException {
        return copy((ReadableByteChannel)in, (WritableByteChannel)out, bufferSize, (StreamProgress)null);
    }

    public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize, StreamProgress streamProgress) throws IORuntimeException {
        Assert.notNull(in, "InputStream is null !", new Object[0]);
        Assert.notNull(out, "OutputStream is null !", new Object[0]);
        ByteBuffer byteBuffer = ByteBuffer.allocate(bufferSize <= 0 ? 8192 : bufferSize);
        long size = 0L;
        if (null != streamProgress) {
            streamProgress.start();
        }

        try {
            while(in.read(byteBuffer) != -1) {
                byteBuffer.flip();
                size += (long)out.write(byteBuffer);
                byteBuffer.clear();
                if (null != streamProgress) {
                    streamProgress.progress(size);
                }
            }
        } catch (IOException var8) {
            throw new IORuntimeException(var8);
        }

        if (null != streamProgress) {
            streamProgress.finish();
        }

        return size;
    }

    public static BufferedReader getReader(InputStream in, String charsetName) {
        return getReader(in, Charset.forName(charsetName));
    }

    public static BufferedReader getReader(InputStream in, Charset charset) {
        if (null == in) {
            return null;
        } else {
            InputStreamReader reader;
            if (null == charset) {
                reader = new InputStreamReader(in);
            } else {
                reader = new InputStreamReader(in, charset);
            }

            return new BufferedReader(reader);
        }
    }

    public static BufferedReader getReader(Reader reader) {
        if (null == reader) {
            return null;
        } else {
            return reader instanceof BufferedReader ? (BufferedReader)reader : new BufferedReader(reader);
        }
    }

    public static PushbackReader getPushBackReader(Reader reader, int pushBackSize) {
        return reader instanceof PushbackReader ? (PushbackReader)reader : new PushbackReader(reader, pushBackSize);
    }

    public static OutputStreamWriter getWriter(OutputStream out, String charsetName) {
        return getWriter(out, Charset.forName(charsetName));
    }

    public static OutputStreamWriter getWriter(OutputStream out, Charset charset) {
        if (null == out) {
            return null;
        } else {
            return null == charset ? new OutputStreamWriter(out) : new OutputStreamWriter(out, charset);
        }
    }

    public static String read(InputStream in, String charsetName) throws IORuntimeException {
        FastByteArrayOutputStream out = read(in);
        return StrUtil.isBlank(charsetName) ? out.toString() : out.toString(charsetName);
    }

    public static String read(InputStream in, Charset charset) throws IORuntimeException {
        FastByteArrayOutputStream out = read(in);
        return null == charset ? out.toString() : out.toString(charset);
    }

    public static String read(ReadableByteChannel channel, Charset charset) throws IORuntimeException {
        FastByteArrayOutputStream out = read(channel);
        return null == charset ? out.toString() : out.toString(charset);
    }

    public static FastByteArrayOutputStream read(InputStream in) throws IORuntimeException {
        FastByteArrayOutputStream out = new FastByteArrayOutputStream();
        copy((InputStream)in, (OutputStream)out);
        return out;
    }

    public static FastByteArrayOutputStream read(ReadableByteChannel channel) throws IORuntimeException {
        FastByteArrayOutputStream out = new FastByteArrayOutputStream();
        copy(channel, Channels.newChannel(out));
        return out;
    }

    public static String read(Reader reader) throws IORuntimeException {
        StringBuilder builder = StrUtil.builder();
        CharBuffer buffer = CharBuffer.allocate(8192);

        try {
            while(-1 != reader.read(buffer)) {
                builder.append(buffer.flip().toString());
            }
        } catch (IOException var4) {
            throw new IORuntimeException(var4);
        }

        return builder.toString();
    }

    public static String readUtf8(FileChannel fileChannel) throws IORuntimeException {
        return read(fileChannel, CharsetUtil.CHARSET_UTF_8);
    }

    public static String read(FileChannel fileChannel, String charsetName) throws IORuntimeException {
        return read(fileChannel, CharsetUtil.charset(charsetName));
    }

    public static String read(FileChannel fileChannel, Charset charset) throws IORuntimeException {
        MappedByteBuffer buffer;
        try {
            buffer = fileChannel.map(MapMode.READ_ONLY, 0L, fileChannel.size()).load();
        } catch (IOException var4) {
            throw new IORuntimeException(var4);
        }

        return StrUtil.str(buffer, charset);
    }

    public static byte[] readBytes(InputStream in) throws IORuntimeException {
        return readBytes(in, true);
    }

    public static byte[] readBytes(InputStream in, boolean isCloseStream) throws IORuntimeException {
        FastByteArrayOutputStream out = new FastByteArrayOutputStream();
        copy((InputStream)in, (OutputStream)out);
        if (isCloseStream) {
            close((Closeable)in);
        }

        return out.toByteArray();
    }

    public static byte[] readBytes(InputStream in, int length) throws IORuntimeException {
        if (null == in) {
            return null;
        } else if (length <= 0) {
            return new byte[0];
        } else {
            byte[] b = new byte[length];

            int readLength;
            try {
                readLength = in.read(b);
            } catch (IOException var5) {
                throw new IORuntimeException(var5);
            }

            if (readLength > 0 && readLength < length) {
                byte[] b2 = new byte[length];
                System.arraycopy(b, 0, b2, 0, readLength);
                return b2;
            } else {
                return b;
            }
        }
    }

    public static String readHex(InputStream in, int length, boolean toLowerCase) throws IORuntimeException {
        return HexUtil.encodeHexStr(readBytes(in, length), toLowerCase);
    }

    public static String readHex28Upper(InputStream in) throws IORuntimeException {
        return readHex(in, 28, false);
    }

    public static String readHex28Lower(InputStream in) throws IORuntimeException {
        return readHex(in, 28, true);
    }

    public static <T> T readObj(InputStream in) throws IORuntimeException, UtilException {
        if (in == null) {
            throw new IllegalArgumentException("The InputStream must not be null");
        } else {
            try {
                ObjectInputStream ois = new ObjectInputStream(in);
                T obj = ois.readObject();
                return obj;
            } catch (IOException var3) {
                throw new IORuntimeException(var3);
            } catch (ClassNotFoundException var4) {
                throw new UtilException(var4);
            }
        }
    }

    public static <T extends Collection<String>> T readUtf8Lines(InputStream in, T collection) throws IORuntimeException {
        return readLines(in, CharsetUtil.CHARSET_UTF_8, collection);
    }

    public static <T extends Collection<String>> T readLines(InputStream in, String charsetName, T collection) throws IORuntimeException {
        return readLines(in, CharsetUtil.charset(charsetName), collection);
    }

    public static <T extends Collection<String>> T readLines(InputStream in, Charset charset, T collection) throws IORuntimeException {
        return readLines(getReader(in, charset), (Collection)collection);
    }

    public static <T extends Collection<String>> T readLines(Reader reader, T collection) throws IORuntimeException {
        readLines(reader, collection::add);
        return collection;
    }

    public static void readUtf8Lines(InputStream in, LineHandler lineHandler) throws IORuntimeException {
        readLines(in, CharsetUtil.CHARSET_UTF_8, lineHandler);
    }

    public static void readLines(InputStream in, Charset charset, LineHandler lineHandler) throws IORuntimeException {
        readLines(getReader(in, charset), (LineHandler)lineHandler);
    }

    public static void readLines(Reader reader, LineHandler lineHandler) throws IORuntimeException {
        Assert.notNull(reader);
        Assert.notNull(lineHandler);
        BufferedReader bReader = getReader(reader);

        try {
            String line;
            while((line = bReader.readLine()) != null) {
                lineHandler.handle(line);
            }

        } catch (IOException var5) {
            throw new IORuntimeException(var5);
        }
    }

    public static ByteArrayInputStream toStream(String content, String charsetName) {
        return toStream(content, CharsetUtil.charset(charsetName));
    }

    public static ByteArrayInputStream toStream(String content, Charset charset) {
        return content == null ? null : toStream(StrUtil.bytes(content, charset));
    }

    public static ByteArrayInputStream toUtf8Stream(String content) {
        return toStream(content, CharsetUtil.CHARSET_UTF_8);
    }

    public static FileInputStream toStream(File file) {
        try {
            return new FileInputStream(file);
        } catch (FileNotFoundException var2) {
            throw new IORuntimeException(var2);
        }
    }

    public static ByteArrayInputStream toStream(byte[] content) {
        return content == null ? null : new ByteArrayInputStream(content);
    }

    public static BufferedInputStream toBuffered(InputStream in) {
        return in instanceof BufferedInputStream ? (BufferedInputStream)in : new BufferedInputStream(in);
    }

    public static BufferedOutputStream toBuffered(OutputStream out) {
        return out instanceof BufferedOutputStream ? (BufferedOutputStream)out : new BufferedOutputStream(out);
    }

    public static InputStream toMarkSupportStream(InputStream in) {
        if (null == in) {
            return null;
        } else {
            return (InputStream)(!in.markSupported() ? new BufferedInputStream(in) : in);
        }
    }

    public static PushbackInputStream toPushbackStream(InputStream in, int pushBackSize) {
        return in instanceof PushbackInputStream ? (PushbackInputStream)in : new PushbackInputStream(in, pushBackSize);
    }

    public static void write(OutputStream out, boolean isCloseOut, byte[] content) throws IORuntimeException {
        try {
            out.write(content);
        } catch (IOException var7) {
            throw new IORuntimeException(var7);
        } finally {
            if (isCloseOut) {
                close((Closeable)out);
            }

        }

    }

    public static void writeUtf8(OutputStream out, boolean isCloseOut, Object... contents) throws IORuntimeException {
        write(out, CharsetUtil.CHARSET_UTF_8, isCloseOut, contents);
    }

    public static void write(OutputStream out, String charsetName, boolean isCloseOut, Object... contents) throws IORuntimeException {
        write(out, CharsetUtil.charset(charsetName), isCloseOut, contents);
    }

    public static void write(OutputStream out, Charset charset, boolean isCloseOut, Object... contents) throws IORuntimeException {
        OutputStreamWriter osw = null;

        try {
            osw = getWriter(out, charset);
            Object[] var5 = contents;
            int var6 = contents.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                Object content = var5[var7];
                if (content != null) {
                    osw.write(Convert.toStr(content, ""));
                    osw.flush();
                }
            }
        } catch (IOException var12) {
            throw new IORuntimeException(var12);
        } finally {
            if (isCloseOut) {
                close((Closeable)osw);
            }

        }

    }

    public static void writeObjects(OutputStream out, boolean isCloseOut, Serializable... contents) throws IORuntimeException {
        ObjectOutputStream osw = null;

        try {
            osw = out instanceof ObjectOutputStream ? (ObjectOutputStream)out : new ObjectOutputStream(out);
            Serializable[] var4 = contents;
            int var5 = contents.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                Object content = var4[var6];
                if (content != null) {
                    osw.writeObject(content);
                    osw.flush();
                }
            }
        } catch (IOException var11) {
            throw new IORuntimeException(var11);
        } finally {
            if (isCloseOut) {
                close((Closeable)osw);
            }

        }

    }

    public static void flush(Flushable flushable) {
        if (null != flushable) {
            try {
                flushable.flush();
            } catch (Exception var2) {
            }
        }

    }

    public static void close(Closeable closeable) {
        if (null != closeable) {
            try {
                closeable.close();
            } catch (Exception var2) {
            }
        }

    }

    public static void close(AutoCloseable closeable) {
        if (null != closeable) {
            try {
                closeable.close();
            } catch (Exception var2) {
            }
        }

    }

    public static void closeIfPosible(Object obj) {
        if (obj instanceof AutoCloseable) {
            close((AutoCloseable)obj);
        }

    }

    public static boolean contentEquals(InputStream input1, InputStream input2) throws IORuntimeException {
        if (!(input1 instanceof BufferedInputStream)) {
            input1 = new BufferedInputStream((InputStream)input1);
        }

        if (!(input2 instanceof BufferedInputStream)) {
            input2 = new BufferedInputStream((InputStream)input2);
        }

        try {
            int ch2;
            for(int ch = ((InputStream)input1).read(); -1 != ch; ch = ((InputStream)input1).read()) {
                ch2 = ((InputStream)input2).read();
                if (ch != ch2) {
                    return false;
                }
            }

            ch2 = ((InputStream)input2).read();
            return ch2 == -1;
        } catch (IOException var4) {
            throw new IORuntimeException(var4);
        }
    }

    public static boolean contentEquals(Reader input1, Reader input2) throws IORuntimeException {
        Reader input1 = getReader(input1);
        BufferedReader input2 = getReader(input2);

        try {
            int ch2;
            for(int ch = input1.read(); -1 != ch; ch = input1.read()) {
                ch2 = input2.read();
                if (ch != ch2) {
                    return false;
                }
            }

            ch2 = input2.read();
            return ch2 == -1;
        } catch (IOException var4) {
            throw new IORuntimeException(var4);
        }
    }

    public static boolean contentEqualsIgnoreEOL(Reader input1, Reader input2) throws IORuntimeException {
        BufferedReader br1 = getReader(input1);
        BufferedReader br2 = getReader(input2);

        try {
            String line1 = br1.readLine();

            String line2;
            for(line2 = br2.readLine(); line1 != null && line1.equals(line2); line2 = br2.readLine()) {
                line1 = br1.readLine();
            }

            return Objects.equals(line1, line2);
        } catch (IOException var6) {
            throw new IORuntimeException(var6);
        }
    }

    public static long checksumCRC32(InputStream in) throws IORuntimeException {
        return checksum(in, new CRC32()).getValue();
    }

    public static Checksum checksum(InputStream in, Checksum checksum) throws IORuntimeException {
        Assert.notNull(in, "InputStream is null !", new Object[0]);
        if (null == checksum) {
            checksum = new CRC32();
        }

        try {
            in = new CheckedInputStream((InputStream)in, (Checksum)checksum);
            copy((InputStream)in, (OutputStream)(new NullOutputStream()));
        } finally {
            close((Closeable)in);
        }

        return (Checksum)checksum;
    }
}

Bean

public class CmDynamicModelReflect {
    private Integer id;

    private String fileKeyWord;

    private String modelName;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFileKeyWord() {
        return fileKeyWord;
    }

    public void setFileKeyWord(String fileKeyWord) {
        this.fileKeyWord = fileKeyWord;
    }

    public String getModelName() {
        return modelName;
    }

    public void setModelName(String modelName) {
        this.modelName = modelName;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HiJohnnyBoy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值