Java中IO流总结笔记


IO流

|--字节流
        |--字节输入流
    InputStream
int read():一次读取一个字节
int read(byte[] bys):一次读取一个字节数组

|--FileInputStream
|--BufferedInputStream

        |--字节输出流
    OutputStream
void write(int by):一次写一个字节
void write(byte[] bys,int index,int len):一次写一个字节数组的一部分

|--FileOutputStream
|--BufferedOutputStream

    |--字符流
        |--字符输入流
    Reader
int read():一次读取一个字符
int read(char[] chs):一次读取一个字符数组

|--InputStreamReader
|--FileReader:InputStreamReader的子类便捷类
|--BufferedReader
String readLine():一次读取一个字符串

    |--字符输出流
    Writer
void write(int ch):一次写一个字符
void write(char[] chs,int index,int len):一次写一个字符数组的一部分

|--OutputStreamWriter
|--FileWriter:OutputStreamWriter的子类便捷类
|--BufferedWriter
void newLine():写一个换行符

void write(String line):一次写一个字符串

不同IO流的文件拷贝代码

import java.io.*;

public class IOCopyDemo {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		fileStreamCopy("src1.txt", "dest1.txt"); // 字节流FileInputStream,FileOutputStream
		BufferedStreamCopy("src2.txt", "dest2.txt"); // 字节流BufferedInputStream,BufferedOutputStream

		// 字符流OutputStreamWriter,InputStreamReader,FileWriter,FileReader
		ReaderWriterCopy("src3.txt", "dest3.txt");
		BufferedReaderWriterCopy("src4.txt", "dest4.txt"); // 字符流BufferedWriter,BufferedReader
	}

	private static void fileStreamCopy(String src, String dest) throws IOException {
		// TODO Auto-generated method stub
		FileInputStream fis = new FileInputStream(src);
		FileOutputStream fos = new FileOutputStream(dest);

		// 一次读取一个字节
		// int b=0;
		// while((b=fis.read())!=-1) {
		// fos.write(b);
		// }

		// 一次读取一组字节
		byte[] byt = new byte[1024];
		int len;
		while ((len = fis.read(byt)) != -1) {
			fos.write(byt, 0, len);
		}

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

	private static void BufferedStreamCopy(String src, String dest) throws IOException {
		// TODO Auto-generated method stub
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
		// 一次读取一个字节
		// int b=0;
		// while((b=bis.read())!=-1) {
		// bos.write(b);
		// }

		// 一次读取一组字节
		byte[] byt = new byte[1024];
		int len;
		while ((len = bis.read(byt)) != -1) {
			bos.write(byt, 0, len);
		}

		bos.close();
		bis.close();
	}

	private static void ReaderWriterCopy(String src, String dest) throws IOException {
		// TODO Auto-generated method stub
		// InputStreamReader isr = new InputStreamReader(new FileInputStream(src));
		// OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream(src));
		FileReader fr = new FileReader(src);
		FileWriter fw = new FileWriter(dest);

		// 一次读取一个字符
		// int b=0;
		// while((b=fr.read())!=-1) {
		// fw.write(b);
		// }

		// 一次读取一组字符
		char[] ch = new char[1024];
		int len;
		while ((len = fr.read(ch)) != -1) {
			fw.write(ch, 0, len);
		}

		fr.close();
		fw.close();
	}

	private static void BufferedReaderWriterCopy(String src, String dest) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(src));
		BufferedWriter bw = new BufferedWriter(new FileWriter(dest));

		// 一次读取一行数据
		String str = null;
		while ((str = br.readLine()) != null) {
			bw.write(str);
			bw.newLine();
		}

		bw.close();
		br.close();
	}
}

你可能感兴趣的:(JAVA基础)