java---Buffered

//使用BufferedInputStream和BufferedOutputStream实现非文本文件
@Test
public void test1(){
long start = System.currentTimeMillis();
String from = "D:\\流1\\Pe.java";
String to = "D:\\流1\\Pe1.java";
copy(from,to);
long end = System.currentTimeMillis();
System.out.println("花费的时间:"+(end-start));
}


public void copy(String from,String to){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.提供读入,写出的文件
File file1 = new File(from);
File file2 = new File(to);


//2.先创建相应的节点流:FileInputStream,FileOutoutStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);


//3.将创建的节点流的对象作为形参传递缓冲流的构造器中
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);


//4.具体的实现文件复制的操作
byte[] b = new byte[1024];
int len;
while ((len = bis.read(b))!=-1) {
bos.write(b, 0, len);
bos.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//5.关闭相应的流
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void test3(){


BufferedReader br = null;
BufferedWriter bw = null;
try {
File file = new File("D:\\流1\\Pe.java");
File file1 = new File("D:\\流1\\Pe1.java");
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file1);
br = new BufferedReader(br);
bw = new BufferedWriter(fw);
// char[] c = new char[1024];
// int len;
// while ((len = br.read(c))!=-1) {
// String str = new String(c,0,len);
// System.out.println(str);
// }
String str = null;
while ((str = br.readLine())!=null) {
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


}
@Test
public void test4(){
BufferedReader br = null;
try {
InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String str;
while (true) {
System.out.println("请输入字符串:");
str = br.readLine();
//忽略大小写
if(str.equalsIgnoreCase("e")||str.equalsIgnoreCase("e")){
break;
}
//转成大写
String str1 = str.toUpperCase();
System.out.println(str1);

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值