BufferedReader 从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。操作: Reader
BufferedWriter 将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。操作:Writer
URL openStream 和 System.in 非字符输入流 InputStreamReader 参数为 InputStream 字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。
如果是字符流,可以通过 BufferedReader 读取
所以有:
URL url=new URL("http://www.baidu.com");
InputStreamReader isr=new InputStreamReader(url.openStream());
BufferedReader br= new BufferedReader(isr);
String str;
sb=new StringBuffer();
while((str=br.readLine())!=null)
{
sb.append(str);
}
FileReader 继承 InputStreamReader 所以
FileReader fr= new FileReader(f);
BufferedReader br= new BufferedReader(fr); #这里不需要在包装成InputStreamReader
String t;
StringBuffer sb= new StringBuffer();
while ((t=br.readLine())!=null) {
sb.append(t);
}
文件写入
File f= new File("./a.txt");
try{
FileWriter fw= new FileWriter(f);//继承 OutputStreamWriter 此类也提供写入方法
//FileOutputStream _fw= new FileOutputStream("./a.txt");
//OutputStreamWriter fw=new OutputStreamWriter(_fw);
BufferedWriter bw=new BufferedWriter(fw);
bw.write(sb.toString());
bw.close();
fw.close();
}catch(IOException e){
}
//对象流
a a1 = new a("a");
ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream("a.txt"));
oos.writeObject(a1);
oos.close();
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("a.txt"));
a b1 = (a)ois.readObject();
b1.run();
//二进制流
DataOutputStream dos= new DataOutputStream(new FileOutputStream("a.txt"));
dos.writeInt(1000);
dos.close();
DataInputStream dis= new DataInputStream(new FileInputStream("a.txt"));
int i=dis.readInt();
System.out.print(i+1000);