读取文件的工具类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012620150/article/details/81986823

读取文件的工具类:

public class FileUtil {

    public String readToString(String fileName) {
        String encoding = "UTF-8";
        File file = new File(fileName);
        Long filelength = file.length();
        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return new String(filecontent, encoding);
        } catch (UnsupportedEncodingException e) {
            System.err.println("The OS does not support " + encoding);
            e.printStackTrace();
            return null;
        }
    }
}

这里写图片描述

读取test.properties文件,路径是: D:\springtest\src\main\resources\test.properties

        FileUtil fileUtil = new FileUtil();
        String result = fileUtil.readToString("D:\\springtest\\src\\main\\resources\\test.properties");
        System.out.println("result is :"+result);

猜你喜欢

转载自blog.csdn.net/u012620150/article/details/81986823