您的位置:首页 > 编程语言 > Java开发

Java缓冲流 读、写、复制、操作字节

2019-06-10 21:25 232 查看

缓冲流操作字符(文本)(高效)

[code]	//缓冲流操作字符(文本)(高效)
@Test
public void test() throws Exception {
BufferedReader br = new BufferedReader(new FileReader (new File("test4.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("test5.txt")));
//		char[] c = new char[10];
//		int len;
//		while((len = br.read(c))!=-1) {
//			bw.write(c, 0, len);
//		}
String line;
while((line = br.readLine())!=null) {
bw.write(line);
bw.newLine();  //换行
bw.flush();  //刷新缓冲
}
bw.close();
br.close();

}

缓冲流操作字节读、写

[code]	//缓冲流操作字节读、写
@Test
public void test2() throws Exception {
File f1 = new File("test4.txt");
FileInputStream fis = new FileInputStream(f1);
BufferedInputStream bis = new BufferedInputStream(fis);

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("test5.txt")));
byte[] b = new byte[10];
int len;
while((len=bis.read(b))!=-1) {
//			String s = new String(b,0,len);
//			System.out.println(s);
bos.write(b, 0, len);
bos.flush();// 刷新缓冲()
}
bos.close();
bis.close();
fis.close();

}

缓冲流操作字节

[code]	//缓冲流操作字节
@Test
public void test1() throws Exception {
File f1 = new File("test4.txt");
FileInputStream fis = new FileInputStream(f1);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] b = new byte[10];
int len;
while((len=bis.read(b))!=-1) {
String s = new String(b,0,len);
System.out.println(s);
}
bis.close();
fis.close();

}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐