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));
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; }
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!