java中講講BufferedReader的用法
BufferedReader的用法
馬克-to-win:BufferedReader和BufferedInputStream差不多,只不過一個處理字符,一個處理字節(jié)。buffer(緩存)什么的原理都是一樣的。馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。
例:2.3.1
import java.io.*;
public class TestMark_to_win {
public static void main(String args[]) throws Exception {
/* from file to string then to "multiline string" then to console. */
String s2, s3 = new String();
/*Creates a new FileReader, given the name of the file to read from.BufferedReader(Reader in) Create a buffering character-input stream
that uses a default-sized input buffer.*/
BufferedReader in = new BufferedReader(new FileReader("c:/4.txt"));
/*readLine is a method from BufferedReader: Read a line of text.
Returns: A String containing the contents of the line, not including
any line-termination characters, or null if the end of the stream has
been reached */
while ((s2 = in.readLine()) != null) {
s3 += s2 + "\n";
}
System.out.println(s3);
in.close();
}
}
結(jié)果:
qi hello bye97我們
z
97
3.14
例:2.3.2
import java.io.*;
public class TestMark_to_win {
public static void main(String args[]) throws Exception {
// /////////////////////// from StringReader to file:
String s = "你們" + "\n" + "他們我們" + "\n";
String s1 = "";
StringReader in2 = new StringReader(s);
in2.reset();
BufferedReader in4 = new BufferedReader(in2);
while ((s1 = in4.readLine()) != null) {
System.out.println("output" + s1);
}
}
}
結(jié)果:
output你們
output他們我們