【Java常用工具类】IO流相关

一、概念

程序与文件|数组|网络文件|数据库

二、分类

  1. 流向:
    输入流和输出流
  2. 数据:
    字节流:二进制,可以一切文件,包括纯文本,doc 音频 视频等等
    字符流:文本文件,只能处理纯文本
  3. 功能
    节点:包裹源头
    处理:增强功能

三、字符流和字节流与文件

  1. 字节流
  • 输入流:InputStream read(byte[] b) read(byte[] b,int off,int len) close()
    FileInputStream
  • 输出流:OutputStream write(byte[]b) write(byte[] b,int off,int len) flush() close()
    FileOutputStream()
  1. 字符流
  • 输入流:Reader read(char[] cbuf) read(char[] cbuf,int off,int len) close()
    FileReader()
  • 输出流:Writer write(char[] cbuf) write(char[] cbuf,int off,int len) flush() close()
    FileWriter()

四、字节流操作

  1. 读取文件
    建立联系
    选择流
    操作
package com.zixin.learn;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Demo01 {
	public static void main(String[] args) {
		// 建立联系
		File f1 = new File("d://1.sql");
		// 选择流
		InputStream in = null;
		try {
			in = new FileInputStream(f1);
			// 操作不断读取缓冲数组
			byte[] b = new byte[10];
			int len = 0;// 接收 实际读取大小
			while (-1 != (len = in.read(b))) {
				// 输出字节数组
				String info = new String(b, 0, len);
				System.out.println(info);
			}

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

  1. 写入文件
package com.zixin.learn;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Demo02 {

	public static void main(String[] args) {
		File file = new File("d://te.txt");
		OutputStream os = null;
		try {
			//追加形式写入文件
			os = new FileOutputStream(file, true);
			String s = "zixin";
			byte[] data = s.getBytes();
			os.write(data, 0, data.length);
			os.flush();// 强制刷新出去
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (Exception e) {

		} finally {
			if (null != os) {
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

}

  1. 拷贝文件
package com.zixin.learn;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class Demo03 {
	public static void main(String[] args) throws Exception {

		// 建立联系
		File src = new File("d://te.txt");
		File dest = new File("d://te1.txt");
		// 选择流
		InputStream in = new FileInputStream(src);
		OutputStream out = new FileOutputStream(dest);
		// 文件拷贝

		byte[] flush = new byte[1024];
		int len = 0;
		// 读取
		while (-1 != (len = in.read(flush))) {
			// 写出
			out.write(flush, 0, len);
		}
		out.flush();// 强制输出
		// 先打开的后关闭
		out.close();
		in.close();
	}
}

  1. 文件夹的拷贝
public static void copyDirDetail(File src, File dest) throws Exception {
		if (src.isFile()) {
			// 选择流
			InputStream in = new FileInputStream(src);
			OutputStream out = new FileOutputStream(dest);
			// 文件拷贝

			byte[] flush = new byte[1024];
			int len = 0;
			// 读取
			while (-1 != (len = in.read(flush))) {
				// 写出
				out.write(flush, 0, len);
			}
			out.flush();// 强制输出
			// 先打开的后关闭
			out.close();
			in.close();
		} else if (src.isDirectory()) {
			dest.mkdirs();
			// 获取下一级目录|文件
			for (File sub : src.listFiles()) {
				copyDirDetail(sub, new File(dest, sub.getName()));
			}
		}
	}

五、字符流操作

  1. 读取文件
package com.zixin.learn;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Demo05 {

	public static void main(String[] args) {
		File file = new File("d://te.txt");
		FileReader reader = null;
		try {
			reader = new FileReader(file);
			char[] flush = new char[10];
			int len = 0;
			while (-1 != (len = reader.read(flush))) {
				String str = new String(flush, 0, len);
				System.out.println(str);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != reader) {
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}

  1. 写入文件
package com.zixin.learn;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class Demo06 {

	public static void main(String[] args) {
		File file = new File("d://te2.txt");
		Writer wr = null;
		try {
			// 选择流 true表示追加文件
			wr = new FileWriter(file,true);
			String ms = "";
			wr.write(ms);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != wr) {
				try {
					wr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

}

  1. 拷贝文件
package com.zixin.learn;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class Demo07 {

	public static void main(String[] args) {
		File src = new File("d://1.txt");
		File dest = new File("d://2.txt");
		Reader reader = null;
		Writer writer = null;
		try {
			reader = new FileReader(src);

			writer = new FileWriter(dest);
			char[] flush = new char[10];
			int len = 0;
			while (-1 != (len = reader.read(flush))) {
				writer.write(flush, 0, len);
			}
			writer.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(null!=writer) {
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(null!=reader) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

}

六、处理流

字节缓冲流

  • BufferedInputStream
  • BufferedOutputStream

字符缓冲流

  • BufferedReader readerLine()
  • BufferedWriter newLine()
  1. 拷贝文件
package com.zixin.learn;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.InputStream;
import java.io.OutputStream;

public class Demo08 {

	public static void main(String[] args) throws Exception {
		// 建立联系
		File src = new File("d://te.txt");
		File dest = new File("d://te1.txt");
		// 选择流
		InputStream in = new BufferedInputStream(new FileInputStream(src));
		OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
		// 文件拷贝

		byte[] flush = new byte[1024];
		int len = 0;
		// 读取
		while (-1 != (len = in.read(flush))) {
			// 写出
			out.write(flush, 0, len);
		}
		out.flush();// 强制输出
		// 先打开的后关闭
		out.close();
		in.close();

	}

}

  1. 拷贝文件
package com.zixin.learn;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class Demo09 {

	public static void main(String[] args) {
		File src = new File("d://1.txt");
		File dest = new File("d://2.txt");
		BufferedReader reader = null;
		BufferedWriter writer = null;
		try {
			reader = new BufferedReader(new FileReader(src));

			writer = new BufferedWriter(new FileWriter(dest));
			String line = null;
			while (null != (line = reader.readLine())) {
				writer.write(line);
				writer.append("\n");
				writer.newLine();
			}
			writer.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != writer) {
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != reader) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

}

你可能感兴趣的:(java常用工具类)