文件的读取流和书写流

public void read(){

  InputStream in=null;     

 try {

   //创建文件字节流

   in=new FileInputStream("jpg/1.jpg");

 

 int i=0; 

 //一次读取一个字节,返回对去的数据,如果返回-1表示读取完毕 

  while((i=in.read())!=-1){

     System.out.println(i); 

  }      

  byte[] by=new byte[1024];

   int len=0;  

  //一次读取1024字节,將读取的数据存入字节数组,返回本次读取的字节数,返回-1表示读取完毕   

 while((len=in.read(by))!=-1){

    System.out.println(len);  

  }    

  } catch (Exception e) {

   // TODO Auto-generated catch block    

e.printStackTrace();

  }finally{

   try {   

  in.close();  

  } catch (IOException e) {  

   // TODO Auto-generated catch block    

 e.printStackTrace();  

  }   }  }

 public void writer(){

  OutputStream out=null;  

    try {    

//创建文件写入流,true表示追加写入数据,默认为替换写入  

  out=new FileOutputStream("abc.txt",true);  

  //写入文件    out.write("明天可以睡懒觉".getBytes());  

 } catch (Exception e) {   

 // TODO Auto-generated catch block  

  e.printStackTrace();  

 }finally{    

try {     

out.close();  

  } catch (IOException e) {

    // TODO Auto-generated catch block    

 e.printStackTrace();   

 }   }  }  

 public void copyfile(){  

 InputStream in=null;   

OutputStream out=null;   

  try {

   in=new FileInputStream("d:/西西软件园.txt");

   out=new FileOutputStream("abc.txt");  

      byte[] b=new byte[1024];   

 int a=0;

   while((a=in.read(b))!=-1){   

  out.write(b, 0, a);

   }        

  } catch (Exception e) {

   // TODO Auto-generated catch block

   e.printStackTrace();  

 }finally{  

  try {    

 out.close();

    in.close();   

 } catch (IOException e) {  

   // TODO Auto-generated catch block     e.printStackTrace();   

 }   }  }

转载于:https://www.cnblogs.com/ldl454700988/p/6822330.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值