`

java 输入和输出流

    博客分类:
  • Java
阅读更多
package com.amaker.file;

import java.io.File;
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.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.io.Serializable;
import java.io.Writer;

public class IODemo {
	public static void main(String[] args) {
		ReaderDemo01();
	}
	
	public static void OutputStreamDemo01(){
		File file = new File("D:" + File.separator + "test.txt"); // 指定要操作的文件
		OutputStream out = null; // 定义字节输出流对象
		try {
			out = new FileOutputStream(file, true); // 实例化操作的父类对象,可以追加内容
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String info = "Hello World!!!\r\n";// 要打印的信息
		byte b[] = info.getBytes(); // 将字符串变为字节数组
		try {
			out.write(b);// 输出内容
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			out.close(); // 关闭
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	public static void InputStreamDemo01(){
		File file = new File("D:" + File.separator + "test.txt");// 要读取的文件路径
		InputStream input = null; // 字节输入流
		try {
			input = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		byte b[] = new byte[1024];// 开辟byte数组空间,读取内容
		int len = 0;
		try {
			int temp = 0; // 接收每次读取的内容
			while ((temp = input.read()) != -1) {// 如果不为-1表示没有读到底
				b[len] = (byte) temp; // int --> byte
				len++;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(new String(b, 0, len));
	}
	//<=========================================================================================>
	//使用Writer完成文件内容的输出
	public static void WriterDemo01(){
		File file = new File("D:" + File.separator + "test.txt"); // 指定要操作的文件
		Writer out = null; // 定义字节输出流对象
		try {
			out = new FileWriter(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 实例化操作的父类对象
		String info = "Hello World!!!";// 要打印的信息
		try {
			out.write(info);// 输出内容
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			out.close();// 关闭
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	//字符输入流:Reader
	public static void ReaderDemo01(){
		File file = new File("D:" + File.separator + "test.txt");// 要读取的文件路径
		Reader input = null; // 字节输入流
		try {
			input = new FileReader(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		char b[] = new char[1024];// 开辟char数组空间,读取内容
		int len;
		try {
			len = input.read(b);// 读取
			input.close();
			System.out.println(new String(b, 0, len));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		/**
		 * 字节流和字符流的区别:
		 * 字节流没有使用到缓冲区,而是直接操作输出的
		 * 字符流使用到了缓冲区,是通过缓冲区操作输出的。
		 * 明显使用字节流操作会方便一些,例如:图片、电影都是字节保存的
		 */
	}
	
	//打印流:PrintStream(重点)
	//开发中输出数据的时候不要再直接使用OutputStream,而都使用PrintStream类。因为输出方便
	public static void PrintStreamDemo01(){
		File file = new File("D:" + File.separator + "temp.txt");
		OutputStream output;
		try {
			output = new FileOutputStream(file);
			PrintStream out = new PrintStream(output);
			out.print("hello");
			out.print(" world ");
			out.println("!!!");
			out.print("1 X 1 = " + (1 * 1));
			out.println("\n输出完毕");
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		

	}
	//<============================================================================>
	/**
	 * 对象序列化:
	 * 一、对象序列化的时候,要被序列化对象所在的类必须实现java.io.Serializable接口
	 * 二、此接口也属于标识接口,表示可以被序列化
	 * 三、如果现在对象中的某个属性不希望被序列化,则此时就可以使用transient关键字进行声明,例如:
	 * private transient String name;则在进行反序列化操作的时候,所有内容的返回值都是默认值。
	 */
	public static void ObjectOutputStreamDemo (){
		ObjectOutputStream oos;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(
					new File("D:" + File.separator + "person.ser")));
			//Person per = new Person("张三", 30);
			//oos.writeObject(per);// 输出
			oos.close();// 关闭
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		

	}
	//进行反序列化操作:ObjectInputStream
	//此时已经将被序列化的内容读取进来了,实际上序列化序列的只是每个类中的属性,因为各个对象只能靠属性区分。
	public static void ObjectInputStreamDemo(){
		try {
			ObjectInputStream oos = new ObjectInputStream(new FileInputStream(
					new File("D:" + File.separator + "person.ser")));
			//Person p = (Person)oos.readObject() ;
			//System.out.println(p);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//<====================================================================================>
	/**
	 * 序列化一组对象
	 */
	/*Person per[] = { new Person("张三", 30), new Person("李四", 35),
			new Person("王五", 50) };
	ser(per); // 序列化一组对象
	Person p[] = (Person[]) dser(); // 反序列化
	for (int x = 0; x < p.length; x++) {
		System.out.println(p[x]) ; 
	}*/
	
	public static void ser(Object obj) throws Exception { // 所有异常抛出
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
				new File("D:" + File.separator + "person.ser")));
		oos.writeObject(obj);// 输出
		oos.close();// 关闭
	}
	public static Object dser() throws Exception { // 所有异常抛出
		ObjectInputStream oos = new ObjectInputStream(new FileInputStream(
				new File("D:" + File.separator + "person.ser")));
		Object obj = oos.readObject();
		return obj;
	}



}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics