Java学习日志(20-2-IO流-Properties与流合并切割)

Properties简述

    Properties是hashtable的子类

    具备map集合的特点,储存的键值对都是字符串

    是集合和IO技术相结合的集合容器

    可以用于键值对形式的配置文件

Properties存取

	// 设置和获取元素
	public static void setAndGet(){
		Properties prop=new Properties();
		prop.setProperty("zhangsan","30");
		prop.setProperty("lisi","39");
		// System.out.println(prop);
		// String value=prop.getProperty("lisi");
		// System.out.println(value);
		
		prop.setProperty("lisi","89");
		
		Set<String>names=prop.stringPropertyNames();
		for(String s:names){
			System.out.println(s+":"+prop.getProperty(s));
		}
	}

Properties存取配置文件

	public static void loadDemo()throws IOException{
		Properties prop=new Properties();
		FileInputStream fis=new FileInputStream("info.txt");
		// 将流中的数据加载进集合
		prop.load(fis);
		prop.setProperty("wangwu","39");
		FileOutputStream fos=new FileOutputStream("info.txt");
		// 将改变的数据存入源文件并添加注释信息
		prop.store(fos,"haha");
		// System.out.println(prop);
		prop.list(System.out);
		fos.close();
		fis.close();
	}
	
	// 将流中的数据存储到集合中
	// 想要将info.txt中的键值数据存到集合中进行操作
	/* 
		1.用一个流和文件关联
		2.读取一行数据,将该行数据用“=”切割
		3.等号左边作为键,右边为值,存入Properties
	*/
	public static void method_1()throws IOException{
		BufferedReader bufr=new BufferedReader(new FileReader("info.txt"));
		String line=null;
		Properties prop=new Properties();
		while((line=bufr.readLine())!=null){
			String[]arr=line.split("=");
			// System.out.println(arr[0]+"____"+arr[1]);
			prop.setProperty(arr[0],arr[1]);
		}
		bufr.close();
		System.out.println(prop);
	}

Properties练习

/* 
练习:
用于记录应用程序运行次数
如果次数已到,那么给出注册提示
原理:
计数器
该计数器定义在程序中,随着程序的运行而在内存中存在,并自增
可是随着该应用程序的退出,该计数器也在内存中消失
下一次启动又重新开始从0计数

本练习中程序即使结束,该计数器的值也存在。
下次程序启动会先加载该计数器的值并+1重新存储
需要建立一个配置文件,用于记录次数

该配置文件使用键值对的形式
便于阅读操作数据
键值对数据是map集合
数据以文件形式存储,使用IO技术
map+io---->Properties
配置文件可以实现应用程序的共享
 */
 
 import java.io.*;
 import java.util.*;
 class RunCount{
	 public static void main(String[] args)throws IOException{
		 Properties prop=new Properties();
		 
		 File file=new File("count.ini");
		 if(!file.exists()){
			 file.createNewFile();
		 }
		 
		 FileInputStream fis=new FileInputStream(file);
		 prop.load(fis);
		 
		 int count=0;
		 String value=prop.getProperty("time");
		 if(value!=null){
			 count=Integer.parseInt(value);
			 if(count>=5){
				 System.out.println("您好,午时已到!");
			 }
		 }
		 count++;
		 prop.setProperty("time",count+"");
		 
		 FileOutputStream fos=new FileOutputStream(file);
		 prop.store(fos,"");
		 fos.close();
		 fis.close();
		 
	 }
 }

打印流PrintWriter/PrintStream

/* 
打印流
提供了打印方法,可以将各种数据类型的数据都原样打印

字符打印流
PrintWriter
构造函数可以接收的参数类型;
1.file对象File
2.字符串路径String
3.字节输出流OutputStream
4.字符输出流Writer

字节打印流
PrintStream
构造函数可以接收的参数类型:
1.file对象File
2.字符串路径String
3.字节输出流OutputStream
 */
 import java.io.*;
 class PrintStreamDemo{
	 public static void main(String[] args)throws IOException{
		 BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
		 PrintWriter out=new PrintWriter(new FileWriter("PSD.txt"),true);
		 String line=null;
		 while((line=bufr.readLine())!=null){
			 if("over".equals(line)){
				 break;
			 }
			 out.println(line.toUpperCase());
			 // out.flush();
		 }
		 out.close();
		 bufr.close();
	 }
 }

合并流/序列流

import java.io.*;
import java.util.*;
class SequenceDemo{
	public static void main(String[] args)throws IOException{
		Vector<FileInputStream>v=new Vextor<FileInputStream>();
		v.add(new FileInputStream("c:\\1.txt"));
		v.add(new FileInputStream("c:\\2.txt"));
		v.add(new FileInputStream("c:\\3.txt"));
		
		Enumeration<FileInputStream>en=v.elements();
		SequenceInputStream sis=new SequenceInputStream(en);
		FileOutputStream fos=new FileOutputStream("c:\\4.txt");
		byte[]buf=new byte[1024];
		int len=0;
		while((len=sis.read(buf))!=null){
			fos.write(buf,0,len);
		}
		fos.close();
		sis.close();
	}
}

切割文件

import java.io.*;
import java.util.*;
class SplitFile{
	public static void main(String[] args)throws IOException{
		
	}
	// 方法二
	public static void merge()throws IOException{
		ArrayList<FileInputStream>al=new ArrayList<FileInputStream>();
		for(int x=1;x<=3;x++){
			al.add(new FileInputStream("c:\\splitFiles\\"+x+".part"));
		}
		Final Iterator<FileInputStream>it=al.iterator();
		Enumeration<FileInputStream>en=new Enumeration<FileInputStream>(){
			public boolean hasMoreElements(){
				return it.hasNext();
			}
			public FileInputStream nextElement(){
				return is.next();
			}
		}
		SequenceInputStream sis=new SequenceInputStream();
		FileOutputStream fos=new FileOutputStream("c:\\splitFiles\\0.bmp");
		byte[]buf=new byte[1024*1024];
		int len=0;
		while((len=sis.read(buf))!=-1){
			fos.write(buf,0,len);
		}
		fos.close();
		sis.close();
	}
	
	// 切割文件
	public static void splitFile()throws IOException{
		FileInputStream fis=new FileInputStream("c:\\1.bmp");
		FileOutputStream fos=null;
		byte[]buf=new byte[1024*1024];
		int len=0;
		int count=1;
		while((len=sis.read(buf))!=-1){
			fos=new FileOutputStream("c:\\split\\1.part"+count+".part");
			fos.write(buf,0,len);
			fos.close();
		}
		if(fos!=null){
			fos.close();
		}
		fis.close();
	}
}

 

转载于:https://my.oschina.net/Almon/blog/755085

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值