黑马程序员——IO流--Properties类、打印流等常见类

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

一 Properties类

表示一个持久属性集,可保存在流中或从流中加载,属性列表中每个键和值对应的值都是一个字符串。

以键值对存在的配置文件

1、Properties存取值:

<span style="font-size:18px;">Properties prop = new Properties();
prop.setProperty("zhangsan1", "31");

//取出所有元素
Set<String> names= prop.stringPropertyNames();
for(String name:names){
String value=prop.getProperty(name);
}</span>
2、元素修改:

prop.setProperty("zhangsan2""31"); //键相同,值覆盖

3、 // 将集合中字符串键值对持久化储存到文件中

FileOutputStream fos = new FileOutputStream("demo.txt");

prop.store(fos, "name+age");//集合存储到文件,store()

fos.close();

4、 //将已有的键值对信息放在集合中

Properties prop = new Properties();

//集合中数据来自文件,文件中数据必须是键值对,使用读取流

FileInputStream fis = new FileInputStream("demo.txt");

prop.load(fis);   //使用load方法

properties.list(System.out);  //输出显示

5.对已有文件中信息进行修改

	<span style="font-size:18px;">//对已有配置修改信息
		//读取文件,将文件存到集合中,通过集合对数据进行修改
		//通过流,将修改后的数据存到文件中
		File file= new File("1.txt");
		if(!file.exists()){
			file.createNewFile();
		}
		FileReader fr=new FileReader("1.txt");
		//创建集合存储配置信息
		Properties properties=new Properties();
		properties.load(fr);
		properties.list(System.out);//输出查看
		properties.setProperty("zhangsan", "22");
		
		//将集合存到文件中
		FileWriter fWriter=new FileWriter(file);
		properties.store(fWriter, "信息");
		fWriter.close();
		fr.close();</span>
6. * 定义功能:获取应用程序运行次数,超过5次,使用次数已到 ,注册提示
<span style="font-size:18px;">	//将配置文件封装成file对象
		File file = new File("1.properties");
		if(!file.exists()){
			file.createNewFile();
		}
		FileReader fr=new FileReader(file);
		Properties properties = new Properties();
		properties.load(fr);
		//通过键获取次
		String value =properties.getProperty("value");
		//定义计数器
		int count=0;
		if(value!=null){
			count=Integer.parseInt(value);//将String类型的value值变为int型
			if(count>=5){
				System.out.println("到了");
			}
		}
		count++;
		//将改变后的次数重新存储到集合中
		properties.setProperty("time", count+"");
		
		FileWriter fw= new FileWriter(file);
		properties.store(fw, "");
		
		fw.close();
		fr.close();</span>
二、PrintStream与PrintWriter:打印流

* 1,提供了打印方法可以对多种数据类型值进行打印。并保持数据的表示形式。 

 打印的所有字符都是平台默认的字符编码转换成字节

 写入字符时使用PrintWriter

 2、PrintStream:可对多种类型进行打印,并保持表示形式

 它不抛IOException.

  构造函数,接收三种类型的值:

 * 1,字符串路径。

 * 2File对象。

 * 3,字节输出流

 PrintStream out = new PrintStream();

 out.print(97);  //97变成字符保持原样将数据打印到目的地

 out.write(610);//只写最低8

3、PrintWriter字符打印流 PrintWriter(out,true); //自动刷新

 * 构造函数参数:

  * 1,字符串路径。

 * 2File对象。

 * 3,字节输出流。

 * 4,字符输出流。

                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));   //读取
		PrintWriter pw = new PrintWriter(System.out);  //写入
		String line =null;
		while((line=br.readLine())!=null){
			if("over".equals(line)){
				break;
			}
			pw.write(line.toUpperCase()+"\r\n");
			pw.flush();
		}
		pw.close();
		br.close();

 三、SequenceInputStream序列流 

 对多个流进行合并

	<span style="font-size:18px;">//创建集合
		Vector<FileInputStream> f1=new Vector<FileInputStream>();
		//存入集合
		f1.add(new FileInputStream("1.txt"));
		f1.add(new FileInputStream("2.txt"));
		f1.add(new FileInputStream("3.txt"));
		Enumeration<FileInputStream> en = f1.elements(); //获取枚举对象
		//合并流
		SequenceInputStream sis= new SequenceInputStream(en);
		//写入目的
		FileOutputStream fos = new FileOutputStream("4.txt");
		byte[] buf = new byte[1024];
		int len =0;
		while ((len=sis.read(buf))!=-1) {
			fos.write(buf,0,len);
		}
		
		fos.close();
		sis.close();</span>
四、IO操作练习
1、文件分割:
<span style="font-size:18px;">                //读取流关联文件
		FileInputStream fis =new FileInputStream("1.txt");
		byte[] buf=new byte[1024];
		//创建目的
		FileOutputStream fos =null;
		int len=0;
		int count =1;
		
		File dir = new File("d:\\");
		if(!dir.exists()){
			dir.mkdirs();
		}
		while ((len=fis.read(buf))!=-1) {
			fos = new FileOutputStream(<span style="color:#ff0000;">new File(dir,(count++)+".txt")</span>);
		}
		fos.close();
		fis.close();</span>
2、合并文件:

<span style="font-size:18px;">	ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
		for(int x=1;x<=3;x++){
			al.add(new FileInputStream(new File(dir,x+".part")));
		}
		Enumeration<FileInputStream> en = Collections.enumeration(al);
		SequenceInputStream sis = new SequenceInputStream(en);
		
		FileOutputStream fos = new FileOutputStream(new File(dir,"1.bmp"));
		byte[] buff=new byte[1024];
		int len=0;
		while ((len=sis.read(buff))!=-1) {
			fos.write(buff,0,len);
		}
		fos.close();
		sis.close();</span>
3、文件中写入对象:  ObjectOutputStream (非静态对象,非瞬态 transient)

静态、瞬时对象无法写入内存

非静态数据不想被序列化可用transient修饰

对象序列化,被序列化的对象必须实现Serializable接口

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.object"));

oos.writeObject(new Person("张三",32));

oos.close();

读取对象(需要存在person类)

ObjectInputStreamobectOutputStream写入的对象进行反序列化

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.object"));

Person p=(Person)ois.readObject();

System.out.println(p);

ois.close();

Serializable:用于给序列化的类加入ID号,用于判断类和对象是否是同一个版本

private static final long serialVersionUID= ;

四、RandomAccessFile 随机访问文件

特点:1、可以读写

2、该对象内部维护一个byte数组,通过指针操作元素

3、可以使用getFilePointer获取指针的位置,可以通过seek设置指针位置

4、就是将字节输入输出进行了封装

5、该对象的源或者目的只能是文件

RandomAccessFile raf=new RandomAccessFile("33.txt""rw");

raf.seek(3*8); //指定位置写入

raf.write("账单".getBytes());

raf.writeInt(98);//保证原字节数不变

 

RandomAccessFile raf=new RandomAccessFile("33.txt""r");

raf.seek(1*8);

byte[] buff =new byte[4];

raf.read(buff);

String name=new String(buff);

int age =raf.readInt();

============================================

五、PipedInputStream PipedOutputStream管道流

输入输出可以直接连接,必须结合多线程

PipedInputStream input1 =new PipedInputStream();

PipedOutputStream out=new PipedOutputStream();

input1.connect(out); //连接两个流

 

new Thread(new Input(input1)).start();

new Thread(new Output(out)).start();

==============================================

操作基本数据类型:写入输出流 DataOutputStream(output);

DataInputStream

操作字节数组ByteArrayInputStream(byte[] buf)ByteArrayOutputStream

关闭ByteArrayOutputStream无效

操作字符数组CharArrayReaderCharArrayWrite

操作字符串  StringReaderStringWriter







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值