BufferedOutputStream与BufferedInputStream
程序员文章站
2024-03-04 20:48:30
...
喜欢在下面评论区扣1支持下呗
字节缓冲输出流: BufferedOutputStream (OutputStream out)
public class BufferStreamDemo {
public static void main(String[] args) throws IOException {
//字节缓冲输出流: BufferedOutputStream (OutputStream out)
FileOutputStream fos = new FileOutputStream( "demo.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
//可将上面两句转化为一句
//BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream( "demo.txt"));
//写数据
bos.write( "hello\r\n".getBytes());
bos.write( "world\r\n".getBytes());
//释放资源
bos.close();
}
}
字节缓神输入流: BufferedInputStream (InputStream in)
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
//字节缓冲输入流: BufferedInputStream (InputStream in)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("demo.txt"));
//方法一:一次读取一个字节数据.
int x;
while ((x=bis.read())!=-1) {
System.out.print((char)x);//直接输出bis.read()出现乱码
}
//方法二:一次读取一个字节数组数据
byte[] yy = new byte[1024];
int y;
while ((y=bis.read(yy))!=-1) {
System.out.print(new String(yy,0,y));
}
//释放资源
bis.close();
}
}
推荐阅读
-
BufferedOutputStream与BufferedInputStream
-
ADO.NET中的五个主要对象的详细介绍与应用
-
字节缓冲流——BufferedOutputStream和BufferedInputStream
-
Asp.net利用JQuery AJAX实现无刷新评论思路与代码
-
Java_基础—BufferedInputStream和BufferOutputStream拷贝
-
浅谈Android Activity与Service的交互方式
-
Android App应用启动分析与优化
-
Java IO流之BufferedInputStream和BufferedOutputStream分析
-
Python绑定方法与非绑定方法详解
-
Android6.0来电号码与电话薄联系人进行匹配