欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

I/O流

程序员文章站 2022-04-09 08:01:48
...

①以程序为参照
向程序中写数据,即输入流;InputStream/Reader 方法:read()
从程序向外写数据,即输出流;OutputStream/Writer 方法:write()

②在关闭输出流之前,应调用flush()方法将缓冲区数据全部写出.

字节流

一个字节一个字节读取/写出
输入流:InputStream
方法:
int read() //读取一个字节
int read(byte[] buffer)
int read(byte[] buffer,int offset,int length)
void close()
long skip(long n)
输出流:OutputStream
void write(int b)
void write(byte[] b)
void write(byte[] b,int off,int len)
void close()
void flush()//将输出流中缓冲的数据全部写出到目的地

字符流

一个字符一个字符读取/写出
输入流:Reader
int read()//读取一个字符
int read(char[] cbuf)
(省略其他,参照InputStream)
输出流:Writer
void flush()
(省略其他,参照OutputStream)

缓冲流

BufferedReader
readLine();//一次读取一行字符,返回字符串类型

BufferedWriter

转换流

OutputStreamWriter/InputStreamReader

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(“d://dxxx.txt”,true),“GBK”);

//可以直接写入字符串,不用一个字符一个字符写入

数据流

DataOutputStream/DataInputStream

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);

dos.writeDouble(3.333);
dos.writeBoolean(true);

ByteArrayInputStream bais = new ByteArrayInputStream();
//bais.available(); 可读取字节数

DataInputStream dis = new DataInputStream(bais);
dis.readDouble();
dis.readBoolean();

//注意:先写先读

打印流

PrintStream/PrintWriter
自带flush()功能,但,手动加上flush()更为保险

可以打印log

Object流

ObjectInputStream/ObjectOutputStream
可用来直接读取/写入 对象.

实现序列化

transient 标记属性时,序列化时忽略该属性.


总结归类

InputStream/OutputStream
Reader/Writer

FileInputStream/out
FileReader/writer

BufferedInputStream/out
BufferedReader/writer

BytearrayInputStream/out

InputStreamReader/OutputStreamWriter

DataInputStream/out

PrintStream
PrintWriter

ObjectInputStream/out


测试代码

package com.zquan.memo.base.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Date;

/**
 * 文件操作类
 * @author zq
 *
 */
public class FileOperateUtil {

	public static void main(String[] args) {
		//TestPrintStream2("D:\\dev\\test_data\\io_bak.txt",System.out);
		TestObjectXXXStream();
	}
	
	/**
	 * FileInputStream 输入流/读取数据  read()
	 */
	public static void TestFileInputStream(){
		FileInputStream is = null;
		try {
			is = new FileInputStream("D:\\dev\\test_data\\io.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		int num = 0;
		int b = 0;
		try {
			while((b=is.read())!=-1){
				System.out.print((char)b);
				num ++;
			}
			//关闭流
			is.close();
			System.out.println("总共读取" + num + "个字节");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * FileOutputStream 输出流/写出数据  write()
	 */
	public static void TestFileOutputStream(){
		FileInputStream is = null;
		FileOutputStream os = null;
		 
		try {
			is = new FileInputStream("D:\\dev\\test_data\\io.txt");
			os = new FileOutputStream("D:\\dev\\test_data\\io_bak.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		int b = 0;
		int num = 0;
		try {
			while((b=is.read())!=-1){
				os.write(b);
				num++;
			}
			os.flush();
			os.close();
			is.close();
			System.out.println("文件已复制");
			System.out.println("总共写入" + num + "个字节");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * FileReader 按字符读取
	 */
	public static void TestFileReader(){
		try {
			FileReader fr = new FileReader("D:\\dev\\test_data\\io.txt");
			int b = 0;
			int num = 0;
			while((b=fr.read())!=-1){
				System.out.print((char)b);
				num ++;
			}
			fr.close();
			System.out.println("\n总共写入" + num + "个字符");
		} catch (FileNotFoundException e) {
			System.out.println("文件找不到");
		} catch (IOException e) {
			System.out.println("读取异常");
		}
	}
	/**
	 * FileWriter
	 */
	public static void TestFileWriter(){
		
			try {
				FileWriter fw = new FileWriter("D:\\dev\\test_data\\io_bak.txt");
				for(int b=0;b<2000;b++){
					fw.write(b);
				}
				fw.flush();
				fw.close();
				System.out.println("写出完毕");
			}catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	/**
	 * BufferedWriter/BufferedReader 
	 */
	public static void TestBufferedWriter(){
		try {
			BufferedReader br = new BufferedReader(new FileReader("D:\\dev\\test_data\\io.txt"));
			BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\dev\\test_data\\io_bak.txt"));
			String str = null;
			while((str=br.readLine())!=null){
				bw.write(str);
				bw.newLine();
			}
			bw.flush();
			bw.close();
			br.close();
			System.out.println("文件已复制");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * PrintStream
	 */
	public static void TestPrintStream(){
		try {
			FileOutputStream fos = new FileOutputStream("D:\\dev\\test_data\\io.txt",true);
			PrintStream ps = new PrintStream(fos);
			if(ps != null){
				System.setOut(ps);
			}
			System.out.println("该文字已被写入文件中xxx。");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	/**
	 * PrintStream System.out 作为参数
	 * @param f
	 * @param ps
	 */
	public static void TestPrintStream2(String f,PrintStream ps){
		try {
			BufferedReader br = new BufferedReader(new FileReader(f));
			String s = null;
			while((s=br.readLine())!= null){
				ps.println(s);
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * PrintWriter 记录log
	 */
	public static void TestPrintStream3(){
		String str = null;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		try {
			FileWriter fw = new FileWriter("D:\\dev\\test_data\\io.txt",true);
			PrintWriter pw = new PrintWriter(fw);
			while((str=br.readLine())!=null){
				if(str.equals("exit")) 	break;
				System.out.println(str.toUpperCase());
				pw.println("log:" + str);
				pw.flush();
			}
			pw.println(new Date());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void TestObjectXXXStream(){
		T t = new T();
		t.d = 11;
		
		ObjectOutputStream oos;
		try {
			FileOutputStream fos = new FileOutputStream("D:\\dev\\test_data\\io.txt");
			oos = new ObjectOutputStream(fos);
			oos.writeObject(t);
			oos.flush();
			oos.close();
		
		
			FileInputStream fis = new FileInputStream("D:\\dev\\test_data\\io.txt");
			ObjectInputStream ois = new ObjectInputStream(fis);
			T tr = (T) ois.readObject();
			ois.close();
			System.out.println(tr.a + " " + tr.b +" "+ tr.c + " " + tr.d);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("serial")
	static class T implements Serializable{
		int a =10;
		String b = "abc";
		double c = 44.332;
		transient int d = 33;//transient 序列化时被忽略
	}
	
	
	
}
相关标签: IO流