Home > Java > javaTutorial > Detailed example of how Java reads resources files

Detailed example of how Java reads resources files

黄舟
Release: 2017-07-26 15:03:04
Original
2772 people have browsed it

This article mainly introduces the detailed explanation and implementation code of java reading resources files. When developing projects, we often encounter reading the contents of the folder. Friends in need can refer to it

Detailed explanation and implementation code of java reading resources files

In Java projects, it is often necessary to package resource files and place them in the project, and then read the corresponding files in the project.

Implementation code:


String str = ReadFile.read(getClass().getResourceAsStream("sence/"+file));
Copy after login


  public static String read(InputStream inputStream) {
    BufferedReader reader = null;
    String laststr = "";
    try{

      InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
      reader = new BufferedReader(inputStreamReader);
      String tempString = null;
      while((tempString = reader.readLine()) != null){
        laststr += tempString;
      }
    }catch(Exception e){
      e.printStackTrace();
    }finally{
      if(reader != null){
        try {
          reader.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return laststr;
  }
Copy after login

The above is the detailed content of Detailed example of how Java reads resources files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template