IO流的一些常见流对象操作

1、打印流 PrintWriter

/**
 * 打印流 PrintWriter
 *  构造参数可以接受的类型 
 * File对象 File 
 * 字符串路径 String 
 * 字节输出流 ouputStream 
 * 字符输出流  Writer
 */

<span style="font-size:18px;">public class PrintWriterDemo01 {


	public static void main(String[] args) throws IOException {
			BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
			String  s=null;
			
	
			<span style="color:#33ff33;">// PrintWriter对象
<span style="white-space:pre">			</span>//PrintWriter pWriter=new PrintWriter(System.out);//输出到控制台</span>
			PrintWriter pWriter=new PrintWriter("PrintWriter.txt")<span style="background-color: rgb(255, 255, 255);">;<span style="color:#33ff33;">//打印到指定文件</span></span>
			while ((s=bufferedReader.readLine()) !=null) {
			<span style="color:#33ff33;">//	pWriter.write(s+"\r\n");</span>
				pWriter.println(s);
				pWriter.flush();<span style="color:#33ff33;">//缓冲区刷新</span>
			}
			<span style="color:#33ff33;">//流关闭</span>
			bufferedReader.close();
			pWriter.close();
	}


}</span>


2、 Properties

/**
 * Properties是HanshTable的子类 具备map集合的特点,里面存的键值对都是字符串 是集合和IO相结合的容器
 * 特点:可以用于键值对形式的配置文件 在加载配置文件时,需要数据具有固定格式,键=值。
 */

<span style="font-size:24px;">public class PropertiesDemo02 {

	public static void main(String[] args) throws IOException {
		// demo01();
		demo02();
	}

	public static void demo01() throws IOException {
		BufferedReader bReader = new BufferedReader(new FileReader("f.txt"));
		String s = null;
		Properties pro = new Properties();
		while ((s = bReader.readLine()) != null) {
			String[] sr = s.split("=");<span style="background-color: rgb(51, 255, 51);">// 切割</span>
			pro.setProperty(sr[0], sr[1]);<span style="color:#33ff33;">// 将切割后的数据添加到Properties中</span>
		}

		bReader.close();
		sop(pro);

	}

	public static void demo02() throws IOException {
		Properties pro = new Properties();
		FileInputStream fInputStream = new FileInputStream("f.txt");<span style="color:#33ff33;">// 和文件关联</span>
		<span style="color:#33ff33;">// 将流中的数据加载到集合</span>
		pro.load(fInputStream);
		pro.list(System.out);<span style="color:#33ff33;">//输出</span>
		
		<span style="color:#33ff33;">//修改配置信息,并将它写回文件</span>
		pro.setProperty("ls", "40");
		FileOutputStream  fOutputStream=new FileOutputStream("f.txt");		
		pro.store(fOutputStream, null);<span style="color:#33ff33;">//第二个参数为注释</span>
		
		
		fInputStream.close();
		fOutputStream.close();
	}

	public static void sop(Object obj) {
		System.out.println(obj);
	}
}</span><span style="font-size:18px;color:#33ff33;">
</span>


/**
 * 练习:限定一个程序的运行次数,当程序运行到一定次数以后就不能再运行。 需要通过Properties集合--------配置文件来完成
 * 
 */


<span style="font-size:18px;">public class PropertiesDemo03 {

	public static void main(String[] args) throws IOException {
		Properties prope = new Properties();

		File file = new File("pz.ini");<span style="color:#33ff33;">// 将配置文件封装为对象</span>

		int con = 0;
		if (!file.exists()) {<span style="color:#33ff33;">// 判断配置文件是否存在</span>
			file.createNewFile();<span style="color:#33ff33;">// 创建配置文件</span>
		}
		<span style="color:#33ff33;">// 输入流</span>
		FileInputStream fInputStream = new FileInputStream(file);<span style="color:#33ff33;">// 将文件和流关联</span>

		prope.load(fInputStream);<span style="color:#33ff33;">// 将流加载到集合中</span>

		String s = prope.getProperty("time");<span style="color:#33ff33;">// 获取次数</span>
		if (s != null) {
			con = Integer.parseInt(s);

			if (con >= 5) {
				System.out.println("次数已到上限,请购买");
				return;
			}
			con++;
		} else {
			con++;
		}

		<span style="color:#33ff33;">// 输出流</span>
		FileOutputStream fOutputStream = new FileOutputStream(file);
		prope.setProperty("time", con + "");
		prope.store(fOutputStream, null);<span style="color:#33ff33;">// 写出去</span>

		fInputStream.close();
		fOutputStream.close();
	}

}</span>

3、序列化
/**
* 对象的序列化和反序列化

* @throws Exception

*             被 static和 transient修饰的不能序列化
*/

public class ObjectSerializationDemo01 {



public static void main(String[] args) throws Exception {
// xlh();
fxlh();
}


// 对象序列化


public static void xlh() throws Exception {
ObjectOutputStream outputStream = new ObjectOutputStream(
new FileOutputStream("F:\\abc\\object.txt"));
// 将对象添加到
outputStream.writeObject(new people("张三", 19));
outputStream.writeObject(new people("李三", 18));
outputStream.writeObject(new people("张", 19));
outputStream.writeObject(new people("李", 18));


outputStream.close();


}


// 对象返序列化
public static void fxlh() throws Exception {
ObjectInputStream oInputStream = new ObjectInputStream(
new FileInputStream("F:\\abc\\object.txt"));
people p = null;
while ((p = (people) oInputStream.readObject()) != null) {
System.out.println(p.name + "-----" + p.age);
}


oInputStream.close();


}
}


class people implements Serializable {// 对象的序列化必须要实现接口Serializable,作为可序列化的标志
String name;
int age;


public people(String name, int age) {
this.name = name;
this.age = age;
}
}


4、文件的切割和合并。

public class SequenceDemo02 {



public static void main(String[] args) throws IOException {


//split();
merge();
}


// 切割文件
public static void split() throws IOException {
FileInputStream fInputStream = new FileInputStream("1.mp3");// 和文件相关联


FileOutputStream fOutputStream = null;// 定义一个空的字节输出流


byte[] b = new byte[1024 * 1024];// 定义一个1M的字节数组
int a = 0;
int count = 1;
while ((a = fInputStream.read(b)) != -1) {
fOutputStream = new FileOutputStream("F:\\abc\\" + (count++)
+ ".part");// 每一次循环就建立一个流对象


fOutputStream.write(b, 0, a);
fOutputStream.close();
}


fInputStream.close();


}


// 合并文件


public static void merge() throws IOException {
ArrayList<FileInputStream> aList = new ArrayList<FileInputStream>();// 创建list数组


for (int i = 1; i < 11; i++) {// 循环添加数据
aList.add(new FileInputStream("F:\\abc\\" + i + ".part"));
}


final Iterator<FileInputStream> it = aList.iterator();
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() {// 创建匿名内部类,实现Enumeration接口方法


public boolean hasMoreElements() {
// TODO Auto-generated method stub
return it.hasNext();
}


@Override
public FileInputStream nextElement() {
// TODO Auto-generated method stub
return it.next();
}
};
// 合并文件
SequenceInputStream sInputStream = new SequenceInputStream(en);
FileOutputStream  fOutputStream =new FileOutputStream("F:\\abc\\0.mp3");//文件关联
byte [] b=new byte[1024*1024];
int  a=0;
while ((a=sInputStream.read(b)) !=-1) {
fOutputStream.write(b, 0, a);//写出去
}
sInputStream.close();
fOutputStream.close();

}


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值