Home > Java > javaTutorial > body text

Java method of reading files based on IO stream

高洛峰
Release: 2017-01-11 14:12:12
Original
1785 people have browsed it

The example in this article describes Java's method of reading files based on IO streams. Share it with everyone for your reference, the details are 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);
}
Copy after login

I hope this article will be helpful to everyone in Java programming.

For more articles related to Java’s method of reading files based on IO streams, please pay attention to 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