JAVA_IO_DEMO

java.io中定义了很多流类型,用来实现输入输出功能,从不同的角度来分类,可以分为:

        按数据流的方向:输入流、输出流

        按处理数据单位:字符流、字节流

        按功能的不一样:节点流、处理流

其中分类站在程序的角度理解:

方向:从程序输出,叫输出流(outputwrite---写出去);进入程序,叫输入流(inputread---读进来)

       单位:字符流处理的单元是2个字节的unicode字符;字节流的处理单元是1个字节

       功能:节点流可以从特定的数据源上读取数据(文件,内存,管道);处理流是在存在的流的基础上扩展读写功能



1.文件

package com.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TestFileStream {
	public static void main(String[] args) {
		String srcFilePath = System.getProperty("user.dir") + "/src/dbConfig.properties";
		String destFilePath = System.getProperty("user.dir") + "/src/dbConfig_copy.properties";
		fileInputStream(srcFilePath);
		fileOutputStream(srcFilePath, destFilePath);
		fileReader(srcFilePath);
		fileWritter(srcFilePath, destFilePath);
	}
	
	/**
	 * fileInputStream读(字节流)
	 */
	public static void fileInputStream(String filePath){
		int b = 0; //读取的字节
		int num = 0;
		try {
			FileInputStream fis = new FileInputStream(filePath);
			while ((b = fis.read()) != -1) {
				System.out.print((char) b);
				num++;
			}
			fis.close();
			System.out.println("读取完毕,共读取:" + num + "个字节!");
		} catch (FileNotFoundException e) {
			System.out.println("文件不存在!" + e.getMessage());
			System.exit(-1);
		} catch (IOException e) {
			System.out.println("文件读取异常!" + e.getMessage());
			System.exit(-1);
		}
	}
	
	/**
	 * fileOutputStream写
	 */
	public static void fileOutputStream(String srcFilePath,String destFilePath){
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			//字节输入流
			fis = new FileInputStream(srcFilePath);
			//字节输出流
			fos = new FileOutputStream(destFilePath);
			int b=0;
			while((b=fis.read())!=-1){
				fos.write(b);
			}
			fis.close();
			fos.close();
			System.out.println("文件复制完毕!");
		} catch (FileNotFoundException e) {
			System.out.println("文件不存在!" + e.getMessage());
			System.exit(-1);
		} catch(IOException e){
			System.out.println("文件读取错误!" + e.getMessage());
			System.exit(-1);
		} 
	}
	
	/**
	 * fileRead读(字符流)
	 * InputStream提供的是字节流的读取,而非文本读取
	 * 用Reader读取出来的是char数组或者String
	 * 使用InputStream读取出来的是byte数组
	 */
	public static void fileReader(String filePath){
		FileReader fr =  null;
		int b = 0;
		int num = 0;
		try {
			fr = new FileReader(filePath);
			while ((b = fr.read()) != -1) {
				System.out.print((char)b);
				num++;
			}
			fr.close();
			System.out.println("读取完毕,共读取:"+num+"个字节!");
		} catch (FileNotFoundException e) {
			System.out.println("文件不存在!" + e.getMessage());
			System.exit(-1);
		} catch(IOException e){
			System.out.println("文件读取错误!" + e.getMessage());
			System.exit(-1);
		}
	}
	
	public static void fileWritter(String srcFilePath,String destFilePath){
		FileReader fr = null;
		FileWriter fw = null;
		try {
			//字符输入流
			fr = new FileReader(srcFilePath);
			//字符输出流
			fw = new FileWriter(destFilePath);
			int b=0;
			while((b=fr.read())!=-1){
				fw.write(b);
			}
			fr.close();
			fw.close();
			System.out.println("文件复制完毕!");
		} catch (FileNotFoundException e) {
			System.out.println("文件不存在!" + e.getMessage());
			System.exit(-1);
		} catch(IOException e){
			System.out.println("文件读取错误!" + e.getMessage());
			System.exit(-1);
		}
	}
}



2.字符
package com.io;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

public class TestBufferStrream {
	
	public static void main(String[] args) {
		String srcFilePath = System.getProperty("user.dir") + "/src/dbConfig.properties";
		String destFilePath = System.getProperty("user.dir") + "/src/dbConfig_copy.properties";
		testRead(srcFilePath);
		testWrite(destFilePath);
	}
	
	public static void  testRead(String filePath){
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		
		try {
			fis = new FileInputStream(filePath);
			bis = new BufferedInputStream(fis,20);//默认缓冲大小8192
			
			System.out.print((char)bis.read());
			System.out.println((char)bis.read());
			
			/*BufferedInputStream类调用mark(int readlimit)方法后读取多少字节标记才失效,
			    是取readlimit和BufferedInputStream类的缓冲区大小两者中的最大值,
			    而并非完全由readlimit确定。
			*/
			bis.mark(10);
			
			byte[] bytes = new byte[10];
			for(int i=0;i<=2 && (bis.read(bytes,0,10))!=-1;i++){
				for (int j = 0; j < bytes.length; j++) {
					System.out.print((char)bytes[j]);
				}
			}
			
			System.out.println("--------------------");
			// 读取超过20,将不能reset
			bis.reset();
			for(int i=0;i<=2 && (bis.read(bytes,0,10))!=-1;i++){
				for (int j = 0; j < bytes.length; j++) {
					System.out.print((char)bytes[j]);
				}
			}
			bis.close();
		} catch (FileNotFoundException e) {
			System.out.println("文件不存在!" + e.getMessage());
			System.exit(-1);
		} catch (IOException e) {
			System.out.println("文件读取异常!" + e.getMessage());
			System.exit(-1);
		}
	}
	
	public static void testWrite(String filePath) {
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(filePath));
			for (int i = 0; i < 100; i++) {
				bw.write(String.valueOf(i));
				bw.newLine();
			}
			bw.flush();
			bw.close();
		} catch (FileNotFoundException e) {
			System.out.println("文件不存在!" + e.getMessage());
			System.exit(-1);
		} catch (IOException e) {
			System.out.println("文件读取异常!" + e.getMessage());
			System.exit(-1);
		}
	}

}



3.内存

package com.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class TestMemoryStream {
	public static void main(String[] args) {
		ByteArrayOutputStream baos = null;
		DataOutputStream dos = null;
		ByteArrayInputStream bais = null;
		DataInputStream dis = null;
		
		try {
			//输出流
			baos = new ByteArrayOutputStream();
			dos = new DataOutputStream(baos);
			//将数据写出到内存中
			dos.writeDouble(new Double(3));
			dos.writeBoolean(true);
			baos.close();
			dos.close();
			
			//输入流
			bais = new ByteArrayInputStream(baos.toByteArray());
			dis = new DataInputStream(bais);
			System.out.println("读入数据如:");
			System.out.println(dis.readDouble());
			System.out.println(dis.readBoolean());
			
			bais.close();
			dis.close();
		} catch (FileNotFoundException e) {
			System.out.println("文件不存在!" + e.getMessage());
			System.exit(-1);
		} catch (IOException e) {
			System.out.println("文件读取异常!" + e.getMessage());
			System.exit(-1);
		}
	}
}



4.对象
package com.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

public class TestObjectStream {
	public static void main(String[] args) {

		String filePath = System.getProperty("user.dir")
				+ "/src/dbConfig_copy.properties";

		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		FileInputStream fis = null;
		ObjectInputStream ois = null;

		Person p = new Person("李四", 23, new Date(System.currentTimeMillis()));
		try {
			// 初始输出流
			fos = new FileOutputStream(filePath,true);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(p);
			oos.flush();
			oos.close();

			// 初始输入流
			fis = new FileInputStream(filePath);
			ois = new ObjectInputStream(fis);
			p = (Person) ois.readObject();
			System.out.println(p.getName() + "-" + p.getAge() + "-" + p.getBirthday());
			ois.close();
		} catch (FileNotFoundException e) {
			System.out.println("文件不存在!" + e.getMessage());
			System.exit(-1);
		} catch (IOException e) {
			System.out.println("文件读取异常!" + e.getMessage());
			System.exit(-1);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println("程序执行完毕!");

	}
}

class Person implements Serializable {
	private static final long serialVersionUID = 1748837269851703709L;

	private String name;
	private transient int age;// 不允许被序列化
	private Date birthday;

	Person() {
	}

	Person(String name, int age, Date birthday) {
		this.name = name;
		this.age = age;
		this.birthday = birthday;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

适用:

对于IO的使用要看具体的情况:

@数据来源

读文件操作,使用文件节点流

String byte[] char[],使用内存节点流

        进程之间进行通信,使用管道节点流

@输出若要格式化

print(Writer/Stream)

@需要缓冲

        Buffered(InputStream/OutputStream/Reader/writer)

 @数据格式

               二进制数据:Stream

                 纯文本数据:Reader、Writer

 

需要注意的是:

             流使用之后需要关闭,写入流需要flush刷新此输出流并强制写出所有缓冲的输出字节,在关闭时候,需要考虑到关闭可能引发的异常
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值