黑马程序员 Java文件流读写(续)

---------------------- android培训java培训、期待与您交流! ----------------------

本篇文章说明Properties,SequenceInputStream,ObjectInputStream,RandomAccessFile的用法。

Properties :存储的内容一般是以键值对的形式,读取时需要根据键找到对应的值。

SequenceInputStream:将多个IO流合并成一个IO流

ObjectInputStream:提供了java类和文件的转换,被转换的类必须实现序列化,才能保证文件保存的正确性

RandomAccessFile:提供了文件读和写,是一个独立的类,不同于其它IO流,它的对象只能是文件

import java.util.*;
import java.io.*;

public class PropertiesDemo {
	
	//简单的properties记录程序使用次数,并存储到文件中
	//需要使用到Properties中的store方法,stroe用于修改Properties中内容
	public void proertiesFile ()throws IOException
	{
		Properties prop = new Properties();
		File f = new File("d:\\count.ini");
		if(!f.exists())
		{
			f.createNewFile();
		}
		InputStream is = new FileInputStream(f);
		prop.load(is);
		int time = 0;
		String count = prop.getProperty("time");
		if(count!=null)
		{
			time = Integer.parseInt(count);
			if(time>=5)
			{
				System.out.println("对不起,您的使用次数已经超过5次");
				return;
			}
		}
		time++;
		prop.setProperty("time",time+"");
		OutputStream os=new FileOutputStream(f);
		prop.store(os,"点击次数增加");
		os.close();
		is.close();
	}

	//文件和持久化类对象的转换,持久化对象必须实现Serializable接口
	//为了识别堆中的对象和文件中的对象一致,需要加入序列化UID
	//同时,静态方法和属性不能被序列化
	public void writeObj() throws IOException
	{
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\obj.txt"));
		Person p = new Person("张三",24);
		oos.writeObject(p);
		oos.close();
	}
	public void readObj() throws Exception
	{
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\obj.txt"));
		Person p = (Person)ois.readObject();
		System.out.println(p.getName());
		ois.close();
	}

	//多个文件流合并成一个文件
	public void sequenceFile() throws IOException
	{
		Vector<FileInputStream> vector = new Vector<FileInputStream>();
		vector.add(new FileInputStream("d:\\111.txt"));
		vector.add(new FileInputStream("d:\\222.txt"));
		vector.add(new FileInputStream("d:\\333.txt"));
		Enumeration en = vector.elements();
		SequenceInputStream sis = new SequenceInputStream(en);
		OutputStream os = new FileOutputStream("d:\\444.txt");
		byte [] by = new byte[1024];
		int len=0;
		while((len=sis.read(by))!=-1)
		{
			os.write(by,0,len);
		}
		os.close();
		sis.close();	
	}
	
	//文件随机读写类RandomAccessFile
	public void writeFile()throws Exception
	{
		//构造方法参数r:只读,rw: 读写,w:只写
		RandomAccessFile raf = new RandomAccessFile("111.txt","rw");
		raf.write("张三".getBytes());//将字符串编码成字节数组
		raf.writeInt(31);//写入整数
		raf.close();
	}
	public void readFile()throws Exception
	{
		//构造方法参数r:只读,rw: 读写,w:只写
		//将数据保存到字符数组中读取
		RandomAccessFile raf = new RandomAccessFile("111.txt","r");
		raf.seek(4);      //任意移动数组中的指针
		int age = raf.readInt();
		System.out.println(age);
		raf.close();
	}
	
	public static void main(String[] args) throws Exception
	{
		PropertiesDemo pd = new PropertiesDemo();
		//pd.sequenceFile();
		//pd.proertiesFile();
		//pd.writeObj();
		//pd.readObj();
		pd.writeFile();
		pd.readFile();
	}

}

---------------------- android培训java培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net/heima

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值