java:文本文档和Properties配置文件读取

导读:本篇文章讲解 java:文本文档和Properties配置文件读取,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

前言

Properties配置文件和纯文本文件读取操作

1.文本文档在开发中有两类应用,一类是配置文件(多是Properties),一类是纯文本。

配置文件一般是键值对,如

user = admin
passwort  = I don't know

纯文本文件,

baidu.com
qq.cpm
mail.com
2.Properties读取

给两个静态方法,调用可以自己写一下

//当资源文件读取,资源文件一般放在 resources 文件夹下
public static Properties getProperties(String filePathName) {
        Properties props = new Properties();
        ClassLoader classLoader = FileUtil.class.getClassLoader();
        try (InputStream inputStream = classLoader.getResourceAsStream(filePathName)) {
            props.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return props;
    }

//当外部文件读取
    public static Properties getProperties(String filePathName) {
        Properties props = new Properties();
        try (InputStream inputStream = new FileInputStream(filePathName)) {
            props.load(inputStream);
        } catch (IOException e) {
            e.getMessage();
        }
        return props;
    }

如果想获取对应字段的值可以调用 getProperty(“key”),key可以是user 或者passwort (参见上面配置文件)

3.纯文本读取

纯文本读取分两类:
一类是资源文件,就是打包的时候放到jar包内部的,
另一类是放在jar包外面的

public class FileUtil {
    //读外部文件
    public static List<String> readFile(String filePathName) {
        List<String> lines = new ArrayList<>();
        try (InputStreamReader stream = new InputStreamReader(new FileInputStream(path), "UTF-8")) {
            try (BufferedReader reader = new BufferedReader(stream)) {
                String line ;
                while ((line = reader.readLine()) != null) {
                    lines.add(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }

    //读资源文件
    public static List<String> readResource(String resource) {
        List<String> lines = new ArrayList<>();
        
        ClassLoader classLoader = FileUtil.class.getClassLoader();
        
        try (InputStream resourceAsStream = classLoader.getResourceAsStream(resource)) {
            try (InputStreamReader stream = new InputStreamReader(resourceAsStream, "UTF-8")) {
                try (BufferedReader reader = new BufferedReader(stream)) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        lines.add(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }
}
5.notes

读取外部文件两方法区别:
读取资源时有一个类加载的声明
ClassLoader classLoader = FileUtil.class.getClassLoader(),这段声明中 FileUtil 是类名;
另外输入流方法也不同
InputStream resourceAsStream = classLoader.getResourceAsStream(resource) ,使用的是类加载的方法;
读外部文件需要有文件完整的路径,读资源文件只需要resources文件夹下的文件名。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/16626.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!