Java自学-IO流(3)

Java自学-IO流(3)

1、字节流复制图片

Java自学-IO流(3)

package lesson02;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/24 19:32
 * FileName: CopyJpgDemo
 * Description:
 */
public class CopyJpgDemo {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("D:\\头像\\小灰栗.jpg");
        FileOutputStream fos = new FileOutputStream("1.jpg");

        byte[] bys=new byte[1024];
        int len;
        while((len=fis.read(bys))!=-1){
            fos.write(bys,0,len);
        }

        fis.close();
        fos.close();
    }
}

2、字节缓冲流

Java自学-IO流(3)

package lesson03;

import java.io.*;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/24 19:44
 * FileName: BufferStreamDemo
 * Description:
 */
public class BufferStreamDemo {
    public static void main(String[] args) throws IOException {
//        FileOutputStream fos = new FileOutputStream("bos.txt");
//        BufferedOutputStream bos = new BufferedOutputStream(fos);
//        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));
//
//        bos.write("hello\r\n".getBytes());
//        bos.write("world\r\n".getBytes());
//
//        bos.close();

//        一次读取一个字节数据。
       BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bos.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.print(new String(bys,0,len));
        }
        bis.close();
    }
}

3、字节流复制视频

Java自学-IO流(3)

Java自学-IO流(3)

4、为什么出现字符流

Java自学-IO流(3)

Java自学-IO流(3)

5、编码表

cmd快速打开 计算器的命令:calc

Java自学-IO流(3)

Java自学-IO流(3)

Java自学-IO流(3)

Java自学-IO流(3)

Java自学-IO流(3)

6、字符串中的编码解码问题

Java自学-IO流(3)

package lesson03;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/24 20:32
 * FileName: StringDemo
 * Description:
 */
public class StringDemo {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String s="中国";

//        byte[] bys = s.getBytes();//[-28, -72, -83, -27, -101, -67]
//        byte[] bys = s.getBytes("utf-8");//[-28, -72, -83, -27, -101, -67]
        byte[] bys = s.getBytes("gbk");//[-42, -48, -71, -6]
        System.out.println(Arrays.toString(bys));

//        String ss = new String(bys);
//        String ss = new String(bys,"utf-8");
        String ss = new String(bys,"gbk");
        System.out.println(ss);
    }
}

7、字符流中的编码解码问题

Java自学-IO流(3)

package lesson03;

import java.io.*;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/24 21:09
 * FileName: ConversionStringDemo
 * Description:
 */
public class ConversionStringDemo {
    public static void main(String[] args) throws IOException {
//        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"));
//        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"),"utf-8");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"),"gbk");
        osw.write("中国");
        osw.close();


        InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"),"gbk");
        //一次读取一个字符数据。
        int ch;
        while((ch= isr.read())!=-1){
            System.out.print((char)ch);
        }

        isr.close();
    }
}

8、字符流写数据的5种方式

Java自学-IO流(3)

Java自学-IO流(3)

package lesson04;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/24 21:25
 * FileName: OutputStreamDemo
 * Description:
 */
public class OutputStreamDemo {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"));

//        osw.write(97);
//        osw.flush();
//        osw.write(98);
//        osw.flush();
//        osw.write(99);

//        char[] chs={‘a‘,‘b‘,‘c‘,‘d‘,‘e‘};
//        osw.write(chs);
//        osw.write(chs,1,3);

//        osw.write("abcde");
        osw.write("abcde",1,3);
        osw.close();
    }
}

9、字符流读数据的2种方式

Java自学-IO流(3)

package lesson04;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/24 21:38
 * FileName: InputStreamReaderDemo
 * Description:
 */
public class InputStreamReaderDemo {
    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"));

//        一次读一个字符数据。
//        int ch;
//        while((ch=isr.read())!=-1){
//            System.out.print((char)ch);
//        }

//        一次读一个字符数组的数据。
        char[] chs = new char[1024];
        int len;
        while((len=isr.read(chs))!=-1){
            System.out.print(new String(chs,0,len));
        }
        
        isr.close();
    }
}

10、字符流复制Java文件

Java自学-IO流(3)

package lesson04;

import java.io.*;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/24 21:46
 * FileName: CopyJavaDemo01
 * Description:
 */
public class CopyJavaDemo01{
    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream("1.java"));
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("*.java"));

//        一次读写一个字符数据。
//        int ch;
//        while((ch=isr.read())!=-1){
//            osw.write(ch);
//        }

        //一次读写一个字符数组数据。
        char[] chs=new char[1024];
        int len;
        while((len=isr.read(chs))!=-1){
            osw.write(chs,0,len);
        }
        
        isr.close();
        osw.close();
    }
}

Java自学-IO流(3)

上一篇:排序算法操作指南


下一篇:Senparc.Weixin.MP SDK 微信公众平台开发教程(五):使用Senparc.Weixin.MP SDK