Java基础知识十一:FileInputStream字节输入流读取、文件复制、读取字节数组、复制图片案例,字节缓冲流介绍、复制视频案例,字符串编码与解码

FileInputStream:字节输入流读取

注意:字节输入流,到-1就是文件内容的末尾

1f67c711c22c4eab9ae4ff2e7b38f0d4.png 

//FileInputStream字节输入流读取
public class FileInputStreamDemo5 {
   public static void main(String[] args) throws IOException {
       FileInputStream fis = new FileInputStream("f:\\test\\ps.txt");
       /*fis.read()读数据
         by=fis.read()把读取到的数据赋值给变量by
         !=1判断by的值是不是等于负1
       * */
       int by;
       while((by=fis.read())!=-1){
           System.out.println((char)by);
       }
       fis.close();
   }
}

6、文件复制

注意:其实就是文件读取时,同时再写入文件

//文件复制:其实就是文件读取时,同时再写入文件
public class FileInputStreamDemo6 {
   public static void main(String[] args) throws IOException {
       FileInputStream fis = new FileInputStream("f:\\test\\ps.txt");
       FileOutputStream fos = new FileOutputStream("f:\\test\\ios.txt");

       int by;
       while((by=fis.read())!=-1){
           fos.write(by);
       }
       fis.close();
       fos.close();
   }
}

7、读取字节数组

注意:数组的字节长度通常是1024及其倍数

//读取字节数组
public class FileInputStreamDemo7 {
   public static void main(String[] args) throws IOException {
       FileInputStream fis = new FileInputStream("f:\\test\\ios.txt");
       byte[] bytes = new byte[1024];//长度一般设置为1024及其倍数
       int len;
       while((len=fis.read(bytes))!=-1){
           System.out.print(new String(bytes,0,len));
       }
       fis.close();
   }
}

8、复制图片案例

注意:读取字节的同时,同时存入字节

//读取和写入图片
public class FileInputStreamDemo8 {
   public static void main(String[] args) throws IOException {
       FileInputStream fis = new FileInputStream("f:\\test\\img\\12.png");
       FileOutputStream fos = new FileOutputStream("f:\\test\\image\\12.png");
       byte[] bytes = new byte[1024];
       int len;
       while((len=fis.read(bytes))!=-1){
           fos.write(bytes,0,len);
       }
       fis.close();
       fos.close();
   }
}

9、字节缓冲流

dd816ae55b83474db0f1ca313b5e72f0.png 

//字节缓冲流
public class FileInputStreamDemo9 {
   public static void main(String[] args) throws IOException {
       //创建字节缓冲区输出流
       BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f:\\test\\ps.txt"));
       bos.write("hello\r\n".getBytes());
       bos.write("world\r\n".getBytes());
       bos.close();
       //创建字节缓冲区输入流
       BufferedInputStream bis = new BufferedInputStream(new FileInputStream("f:\\test\\ps.txt"));
       //第一方式,读取单个字节
       int by;
       while((by=bis.read())!=-1){
           System.out.print((char)by);
       }
       //第二种方式读取字节数组
       byte[] bys = new byte[1024];
       int len;
       while((len=bis.read(bys))!=-1){
           System.out.println(new String(bys,0,len));
       }
       bis.close();
   }
}

10、案例:复制视频

//四种字节流方式读写视频
public class FileInputStreamDemo10 {
   public static void main(String[] args) throws IOException {
       FileInputStream fis = new FileInputStream("f:\\test\\vadio1\\2.mp4");
       FileOutputStream fos = new FileOutputStream("f:\\test\\vadio2\\2.mp4")*/;
       //第一种:单个字节流
       int by;
       while((by=fis.read())!=-1){
           fos.write(by);
       }
       //第二种:单个字节数组
       byte[] bytes = new byte[1024];
       int len;
       while((len=fis.read(bytes))!=-1){
           fos.write(bytes,0,len);
       }
       fis.close();
       fos.close();
       
       BufferedInputStream bis = new BufferedInputStream(new FileInputStream("f:\\test\\vadio1\\3.mp4"));
       BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f:\\test\\vadio2\\3.mp4"));
       //第三种:字节缓冲区单个字节
       int by;
       while((by=bis.read())!=-1){
           bos.write(by);
       }
       //第四种:字节缓冲区数组
       byte[] bytes = new byte[1024];
       int len;
       while((len=bis.read(bytes))!=-1){
           bos.write(bytes,0,len);
       }
       bis.close();
       bos.close();
   }
}

d0206d598afd41f5be8efbb2afbef291.png 

0e5d0612ecf24ad9ae299c2216c67544.png 

11、字符串编码与解码

编码格式与解码格式需要对应

UTF-8:三个字符表示一个汉字

GBK: 两个字符表示一个汉字

2b1c912485eb4ccabd45d28a1dee7998.png 

//字符串编码与解码
public class StringDemo1 {
   public static void main(String[] args) throws UnsupportedEncodingException {
       String s = "中国";

      /* 第一种方式编码
       byte[] bys1 = s.getBytes();//系统默认编码格式UTF-8
       System.out.println(Arrays.toString(bys1));
       //第二种方式编码
       byte[] bys2 = s.getBytes("UTF-8");//指定UTF-8格式:一个汉字3个字符
       System.out.println(Arrays.toString(bys2));
       //第三种方式编码
       byte[] bys3 = s.getBytes("GBK");//指定GBK格式:一个汉字2个字符
       System.out.println(Arrays.toString(bys3));*/

      //解码第一种方式:系统默认
       byte[] bys1 = s.getBytes();
       String s1 = new String(bys1);
       System.out.println(s1);
       //解码第二种方式:指定utf-8
       byte[] bys2 = s.getBytes();//系统默认编码就是utf-8
       String s2 = new String(bys2, Charset.forName("utf-8"));
       System.out.println(s2);
       //解码第三种方式:指定GBK
       byte[] bys3 = s.getBytes("gbk");//编码GBK
       String s3 = new String(bys3, Charset.forName("gbk"));//解码GBK
       System.out.println(s3);
   }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜀州凯哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值