1. InputStreamReader和OutputStreamWriter的使用(Decorator)
class="java" name="code">
BufferedReader.readLine()方法可以一次读取一行。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class StreamTest {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("file.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("http://www.google.com");
bw.write("\n");
bw.write("http://www.baidu.com");
bw.close();
FileInputStream fis = new FileInputStream("file.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String str = br.readLine();
while(null != str){
System.out.println(str);
str = br.readLine();
}
br.close();
}
}
public class StreamTest2 {
public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str;
while(null != (str = br.readLine())){
System.out.println(str);
}
br.close();
}
}