Java method of reading files based on IO stream

  • 2020-04-01 04:16:30
  • OfStack

This article illustrates the Java method of reading files based on IO stream. Share with you for your reference, as follows:


public static void readFile(){
String pathString = TEST.class.getResource("/simu").getFile();
try {
  pathString = URLDecoder.decode(pathString, "utf-8");
} catch (UnsupportedEncodingException e1) {
  e1.printStackTrace();
}
System.out.println(pathString);
File file = new File(pathString,"test.txt");
InputStream is = null;
try {
  is = new FileInputStream(file);
} catch (Exception e) {
  e.printStackTrace();
}
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer("");
String b = "";
try {
  while((b=br.readLine())!=null){
    sb.append(b).append("n");
  }
} catch (Exception e) {
  e.printStackTrace();
}
String bb = sb.toString();
System.out.println(bb);
}

I hope this article has been helpful to you in Java programming.


Related articles: