IO之字节流


字节流

           InputStream

           FileInputStream fis = newFileInputStream("demo.txt");

           1.

                   byte[]b = new byte[fis.available];

                   fis.read(b);

                   System.out.println(newString(b));

           2.

                   byte[]buf = new byte[1024];

                   intlen = 0;

                   while((len=fis.read(buf))!=-1){

                            System.out.println(newString(buf,0,len));

                   }

           3.

                   intlen = 0;

                   while((len=fis.read())!=-1){

                            System.out.println((char)len);

                   }

                   OutputStream

                   FileOutputStrdamfos  = newFileOutputStream("demo.txt");

                   fos.write("aabadsd".getBytes());//不用手动刷新,自动刷新

                   fos.close():

                  

文件拷贝

         publicstatic void main(String[] args) throws IOException {

                   Stringsrc = "D:\\love.mp3";

                   Stringdes = "E:\\my.mp3";

                   copyMedio(src,des);

                   copyMedio1(src,des);

         }

 

         public  static void copyMedio1(String src, Stringdes) throws IOException {

                   FileInputStreamfis = new FileInputStream(src);

                   BufferedInputStreambuffis = new BufferedInputStream(fis);

                   FileOutputStreamfos = new FileOutputStream(des);

                   BufferedOutputStreambos = new BufferedOutputStream(fos);

         //      byte[] buf = new byte[BUF_SIZE];

                   intlen = 0;

                   while((len= buffis.read())!=-1){

                            fos.write(len);

                   }

                   System.out.println("拷贝成功");

                   fos.close();

                   fis.close();

         }

 

         privatestatic void copyMedio(String src, String des) throws IOException {

                   FileInputStreamfis = new FileInputStream(src);

                   FileOutputStreamfos = new FileOutputStream(des);

                   byte[]buf = new byte[BUF_SIZE];

                   intlen = 0;

                   while((len= fis.read(buf))!=-1){

                            fos.write(buf,0, len);

                   }

                   System.out.println("拷贝成功");

                   fos.close();

                   fis.close();

                  

         }

从键盘读取数据到控制台

         publicstatic void main(String[] args) throws IOException {

                            InputStreaminput = System.in;//通过系统类获取类中的静态输入流对象

                            Stringdesc = "D:\\t1.txt";//键盘录入的输入流不能手动关,因为就有一个

                            //这样的流。关掉后,就创建不了了

                            KeyWrite(desc,input);

                            //输入流的read方法等待输入,处于阻塞状态

         }

 

         publicstatic void KeyWrite(String desc, InputStream input) throws IOException {

                   BufferedInputStreambis = new BufferedInputStream(input);

                   BufferedWriterbos = new BufferedWriter(new FileWriter(desc,true));

                   byte[]b = new byte[1024];

                   intlen = 0;

                   while((len= bis.read(b))!=-1){

                            bos.write(newString(b,0,len));

                            bos.flush();

                   }

                   bos.close();

         }

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值