OutputStream、InputStream、Writer、Reader学习笔记

本文详细介绍了 Java 中的 IO 流操作,包括字节流与字符流的基本概念、常用类库及其实现方法。通过具体代码示例展示了如何使用 FileOutputStream 和 FileInputStream 进行文件的读写,以及 BufferedWriter 和 BufferedReader 在字符流中的应用。

  学习java.io类库

使用PD画了一张java.io的主要类库  如下




主要的就是字节流与字符流

字节流以OutputStream、InputStream

字符流以Writer、Reader

字节流与字符流的桥梁为OutputStreamWriter、InputStreamReader

缓冲字符流的为BufferedWriter、BufferedReader


2015-9-27 修改



字符流与字节流从上层往下看的时候  只要把顶层的几个接口相关的方法 了解了 其他的 就比较简单了


字节输入流与字节输出流的测试

/**
 * 
 */
package com.undergrowth.io;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Calendar;
import java.util.Locale;

import org.junit.Test;

/**
 * 输出流学习笔记
 * OutputStream(抽象类) 
 *    -->FileOutputStream
 *    -->ObjectOutputStream(在SerializableLearn中进行测试)
 *    -->ByteArrayOutputStream
 *  
 * 输入流学习笔记
 *  InputStream
 *  	-->FileInputStream
 *  	-->ObjectInputStream
 *  	-->ByteArrayInputStream  
 * @author Administrator
 *
 */
public class OutputInputStreamLearn {

	/**
	 * 
	 */
	public OutputInputStreamLearn() {
		// TODO Auto-generated constructor stub
	}
	//输出路径
	String pathname="d:\\log\\";
	File filePath=new File(pathname);
	OutputStream osStream=null;
	ObjectOutputStream ooStream=null;
	InputStream isStream=null;
	ObjectInputStream oisStream=null;
	//测试输出的字符串
	String testOutString="测试输出的FileOutputStream字符串";
	byte[] b=new byte[testOutString.getBytes().length];
	
	
	/**
	 * 测试输出流数据
	 * 文件输出流-->用于将文件写入File或FileDescriptor的输出流
	 */
	@Test
	public void testFileOutputStream(){
		isFileExist(filePath);
		filePath=new File(filePath, getFileName());
		try {
			osStream=new FileOutputStream(filePath);
			osStream.write(testOutString.getBytes());
			osStream.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeOutStream(osStream);
		}
		
	}

	/**
	 * 测试文件输入流
	 */
	@Test
	public void testFileInputStream(){
		isFileExist(filePath);
		filePath=new File(filePath, getFileName());
		try {
			isStream=new FileInputStream(filePath);
			isStream.read(b);
			System.out.println(new String(b));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeInStream(isStream);
		}
	}
	
	
	/**
	 * 测试对象输出流
	 */
	@Test
	public void testObjectOutputStream(){
		isFileExist(filePath);
		filePath=new File(filePath, getFileName());
		try {
			osStream=new FileOutputStream(filePath);
			ooStream=new ObjectOutputStream(osStream);
			ooStream.writeObject(testOutString);
			ooStream.flush();
			osStream.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeOutStream(ooStream);
			closeOutStream(osStream);
		}
	}
	
	/**
	 * 测试对象输入流
	 */
	@Test
	public void testObjectInputStream(){
		isFileExist(filePath);
		filePath=new File(filePath, getFileName());
		try {
			isStream=new FileInputStream(filePath);
			oisStream=new ObjectInputStream(isStream);
			String resultString=(String) oisStream.readObject();
			System.out.println(resultString);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeInStream(oisStream);
			closeInStream(isStream);
		}
	}
	
	/**
	 * 关闭输出流
	 * @param osStream2
	 */
	private void closeOutStream(OutputStream osStream2) {
		// TODO Auto-generated method stub
		if(osStream2!=null){
			try {
				osStream2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 关闭输入流
	 * @param osStream2
	 */
	private void closeInStream(InputStream isStream) {
		// TODO Auto-generated method stub
		if(isStream!=null){
			try {
				isStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 判断文件是否存在
	 * @param filePath2
	 */
	private void isFileExist(File filePath2) {
		// TODO Auto-generated method stub
		if(!filePath2.exists()) filePath2.mkdir();
	}

	/**
	 * 获取文件名
	 * @return
	 */
	private String getFileName() {
		// TODO Auto-generated method stub
		Calendar calendar=Calendar.getInstance(Locale.CHINA);
		String fileName=calendar.get(Calendar.YEAR)+"_"+(calendar.get(Calendar.MONTH)+1)+"_"+calendar.get(Calendar.DAY_OF_MONTH)+"_"+OutputInputStreamLearn.class.getSimpleName()+".txt";
		return fileName;
	}
	
	/**
	 * 测试字节数组输出流
	 * ByteArrayOutputStream-->将构建一个缓冲区,将数据写入字节数组
	 */
	@Test
	public void testByteArrayOutputStream(){
		int size=1000;
		ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(size);
		try {
			byteArrayOutputStream.write(testOutString.getBytes());
			System.out.println(new String(byteArrayOutputStream.toByteArray()));
			//使用平台默认的字符集将字节码转换为字符串
			System.out.println(byteArrayOutputStream.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeOutStream(byteArrayOutputStream);
		}
	}
	
	/**
	 * 打印各种数据值的表现形式
	 */
	@Test
	public void testPrintStream(){
		PrintStream psStream=new PrintStream(System.out);
		psStream.append('Q');
		psStream.println();
		psStream.print(true);
		psStream.println();
		try {
			psStream.write(testOutString.getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}


字符流的输入、输出

package com.undergrowth.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Calendar;
import java.util.Locale;

import org.junit.Test;

/**
 * 字符流学习笔记
 * Writer-->字符流写
 * 		 -->BufferedWriter
 * 		 -->OutputStreamWriter
 * 		 	   -->FileWriter
 * 		 -->PrintWriter
 * 
 * Reader-->字符流读
 * 		 -->BufferedReader
 * 			 -->LineNumberReader
 * 		 -->InputStreamReader
 * 		    -->FileReader
 * 
 * @author Administrator
 *
 */
public class WriterReaderLearn {

	public WriterReaderLearn(){}
	
	/**
	 * 测试字符流写
	 * 使用BufferedWriter进行缓冲字符流
	 */
	@Test
	public void testWriter(){
		PrintWriter pwWriter=null;
		isFileExist(filePath);
		filePath=new File(filePath, getFileName());
		try {
		    pwWriter=new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
			pwWriter.write(testOutString);
			pwWriter.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeWriter(pwWriter);
		}
	}
	
	/**
	 * 测试字符流读
	 * 使用BufferedReader进行缓冲字符流
	 */
	@Test
	public void testReader(){
		BufferedReader brReader=null;
		isFileExist(filePath);
		filePath=new File(filePath, getFileName());
		try {
			StringBuilder builder=new StringBuilder();
			String tmpString=null;
			brReader=new BufferedReader(new FileReader(filePath));
			while ((tmpString=brReader.readLine())!=null) {
				builder.append(tmpString);
			}
			System.out.println(builder.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeReader(brReader);
		}
	}
	
	//输出路径
	String pathname="d:\\log\\";
	File filePath=new File(pathname);
	//测试输出的字符串
	String testOutString="测试输出的WriterReaderLearn字符串";
	
	/**
	 * 关闭读字符流
	 * @param reader
	 */
	private void closeReader(Reader reader){
		if(reader!=null){
			try {
				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 关闭写字符流
	 * @param writer
	 */
	private void closeWriter(Writer writer){
		if(writer!=null){
			try {
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 判断文件是否存在
	 * @param filePath2
	 */
	private void isFileExist(File filePath2) {
		// TODO Auto-generated method stub
		if(!filePath2.exists()) filePath2.mkdir();
	}

	/**
	 * 获取文件名
	 * @return
	 */
	private String getFileName() {
		// TODO Auto-generated method stub
		Calendar calendar=Calendar.getInstance(Locale.CHINA);
		String fileName=calendar.get(Calendar.YEAR)+"_"+(calendar.get(Calendar.MONTH)+1)+"_"+calendar.get(Calendar.DAY_OF_MONTH)+"_"+WriterReaderLearn.class.getSimpleName()+".txt";
		return fileName;
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值