package _03;
import java.io.*;
public class _3_23BufferInputStream {
public static void main(String[] args) {
// 1.性能
// 2.底层还是节点流
// 3.直接关闭处理流,内部自动关闭内部流
// 原理:提供默认的缓存,默认缓存内存为8k,也可以自己设定,当缓存满了则再进行传输
File file = new File("D:\\life\\demo.txt");
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(file));
byte[] flush = new byte[1024]; //容器大小
int length = -1;//接收长度
while((length=ism.read(flush))!=-1){
//将字节数组转换成字符
String temp = new String(flush,0,length,"utf-8");
System.out.print(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package _03;
import java.io.*;
import java.net.URL;
public class _3_23InputStreamReader {
public static void main(String[] args) {
// InputStreamReader 字节流和字符流之间的桥梁,将字节流转成字符流
// 为字节流指定字符集
// 1.操作System.in and System.out
// InputStreamReader isr = new InputStreamReader(System.in);
// try(BufferedReader bis = new BufferedReader( new InputStreamReader(System.in));
// BufferedWriter osw = new BufferedWriter(new OutputStreamWriter(System.out));){ //使用try...with..resouce 不需要释放资源
// 循环获取键盘输入,输出此内容
// String msg = "";
// while(!msg.equals("exit")){
// msg = bis.readLine();
// osw.write(msg);
// osw.newLine();
// osw.flush(); // osw有缓存空间,没有达到缓存空间的容量 它不会输出
// }
// }catch(IOException e){
// e.printStackTrace();
// }
// 2.操作网络流
try(BufferedReader is = new BufferedReader(new InputStreamReader(new URL("http://www.baidu.com").openStream(),"utf-8"));
BufferedWriter ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\baidu.txt"),"utf-8"));){ //网络节点 字节流
// int temp;
// while((temp=is.read())!=-1){
// System.out.print((char)temp);
// }
String msg = "";
while((msg=is.readLine())!=null){
ow.write(msg);
ow.newLine();
}
ow.flush();
}catch(IOException e){
}
}
}