java讀取文件裏面內容(參數、文本)

方法一:Paths類的get方法

   Paths.get裏面文件所在目錄的格式爲:Path path = Paths.get("src","main",.........);

   默認文件所在目錄爲該項目根目錄

public String getFIlesContent(String url) throws IOException {

        Path path = Paths.get("scripts",url);
        byte[] data = Files.readAllBytes(path);
        String content = new String(data,"utf-8");
        return content;
    }

方法二: ResourceBundle類的getBundle方法

    ResourceBundle類通常是用於針對不同的語言來使用的屬性文件。而如果你的應用程序中的屬性文件只是一些配置,並不是針對多國語言的目的。那麼使用Properties類就可以了。

## project.properties

username = user
password = 123
 public Map<String,String> getParmMap(){

        ResourceBundle resourceBundle = ResourceBundle.getBundle("project",new Locale("zh", "CN"));
        Map<String,String> map = new HashMap<>();
        map.put("username",resourceBundle.getString("username"));
        map.put("password",resourceBundle.getString("password"));
        return map;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章