java 之 IO

浅析理论:

一、IO概述(input/output)
	1、输入输出流,字符字节流,字节流一般带Stream,字符流一般是Reader和Writer
	2、所有文件都是二进制序列,在java中使用byte[]可以表示文件内容,其中文本文件内容可以用char[]表示
	3、需要关闭流close()
二、四大顶层抽象类
	1、InputStream字节输入流
		int read();//读到末尾,返回-1
		read(byte[] buff);//最多读取buff.length长度个
		read(byte[] buff, int off, int len);//读取到的数据,第一个保存到buff[off]中,最多读取len个
	2、OutputStream字节输出流
		write(int b);//输出一个字节
		write(byte[] buff);//批量输出字节,输出个数为buff.length
		write(byte[] buff, int off, int len);// 从buff[off]开始输出,输出长度为len
	3、Reader
		int read();
		int read(char[] cbuf);
		int read(char[] cbuf, int off, int len);
		//对于文本文件的读入一般是以行为单位
	4、Writer
		void write(char[] cbuf);
		void writer(char[] cbuf, int off, int len);
		void writer(int a);
		void writer(String str);
		coud writer(String str, int off, int len);
三、各种实现类
	1、InputStream
		FileInputStream
		ByteArrayInputStream
		ObjectInputStream
	2、OutputStream
		FileOutputStream
		ByteArrayOutputStream
		ObjectOutputStream
	3、Reader
		FileReader(x)
		*BufferedReader
		InputStreamReader
	4、Writer
		*PrintWriter
		FileWriter(x)
		OutputStreamWriter
四、非文本文件,比如:图片、视频、exe文件等
	文件内容一般使用byte[]表示,一般使用字节相关流来操作,比如FileInputStream、FileOutputStream,使用while循环
五、文本文件,比如:txt、html、xml
	一般使用字符操作流,常用一行一行读取操作
	文本文件的操作关键点是主意文件的字符编码
	注:要使用和文件一样的字符编码读入,否则会出现乱码
	1、读入:创建带缓冲的输入字符流【存在内存中】
		FileInputStream fis = new FileInputStream("F:/a.txt");
		InputStreamReader isr = new InputStreamReader(fis, "utf-8");
		BufferedReader r = new BufferedReader(isr);
	2、一行一行的读入
		String line = null;
		while((line=r.readLine())!=null) {  }
	3、写入:创建BufferedWriter对象
		FileOutputStream fos = new FileOutputStream("f:/a.txt", true);//true表示是否追加
		OutputStreamWriter osw = OutputStreamWriter(fos, "utf-8");
		BufferedWriter bw = BufferedWriter(osw);
	4、一行一行的写入
		bw.Write(String s);
		bw.newLine();
六、读取控制台
	System.in;
		

  

浅析案例:

package com.gongxy;

import java.io.*;

/**
 * @author gongyg
 * @date 2020/9/3 14:04
 */
public class IOTest {
    public static void main(String[] args) throws Exception{
        //copyPhotoMethod();
        copyTextMethod();
    }

    /*
    复制图片
     */
    static void copyPhotoMethod() throws Exception{
        //读入
        FileInputStream fis = new FileInputStream("g:/测试/测试a/a.png");
        //写入
        FileOutputStream fos = new FileOutputStream("g:/测试/测试B/b.png");

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

    /*
    复制文本文件
     */
    static void copyTextMethod() throws Exception{
        //读入
        FileInputStream fis = new FileInputStream("g:/测试/测试a/a.txt");
        InputStreamReader isr = new InputStreamReader(fis, "utf-8");
        BufferedReader br = new BufferedReader(isr);

        FileOutputStream fos = new FileOutputStream("g:/测试/测试B/b.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
        BufferedWriter bw = new BufferedWriter(osw);

        String line;
        while((line = br.readLine()) != null){
            bw.write(line);
            bw.newLine();
        }
        br.close();
        bw.close();
    }
}

  

posted @ 2020-09-03 16:14  gygtech  Views(141)  Comments(0Edit  收藏  举报