IO流的基本操作

流的基类
按流向区分:
输出流:OutputStream和Write作为基类
输入流:InputStream和Reader作为基类(程序读取文档)
按照流向划分:
流:输入流,输出流
按照处理单元划分:
流 : 字节流 字符流
字符流:字符输入流,字符输出流

字节流:字节输入流,字姐输出流

字节流;以字节单位处理的流(8位)
字符流;以字符单位处理的流(俩个字节16位)
流的基类:
输入流:以InputStream(字节为单位,字节输入流)和Reader(字符为单位,字符输入流) 为基类
输出流:以OutputStream(字节为单位,字节输出流)和Wirter(字符为单位,字符输出流) 为基类

//设置文件位置
    public static final String bookName="E:/iop/tushu/book.txt";
	
    //将map数据生成一个文件
	public static void booksave(Map initial) {
		ObjectOutputStream oos=null;
		try {
			oos=new ObjectOutputStream(new FileOutputStream(bookName));
			oos.writeObject(initial);
//			System.out.println("图书录入成功");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(oos!=null) {
					oos.flush();
					oos.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//将上面存取的数据进行获取
	public static Map bookgetObject() {
		Map list=new HashMap<>();
		ObjectInputStream ois=null;
		try {
			ois=new ObjectInputStream(new FileInputStream(bookName));
			list.putAll((Map)ois.readObject());
		} 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 {
			try {
				if(ois!=null) {
					ois.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

字节流转字符流
                FileInputStream fis=null;
		InputStreamReader isr=null;
		BufferedReader br=null;
			fis=new FileInputStream("E:/iop/NewmyDoc.txt");
			isr=new InputStreamReader(fis);
			br=new BufferedReader(isr);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值