IO流-其他重要的流

1.Properties

来自集合体系
HashTable
java.util.Properties -- 可以从文件中加载,也可以写出到文件中 -- 经常用做程序的配置文件
Properties信息也可以保存在.properties格式的文件中。properties文件中"#"之后的内容是被注释掉了。
Properties中不允许存储中文数据,如果需要保存非ISO8859-1的数据,需要保存成\uxxxx格式

Properties() 
Object setProperty(String key, String value) 
String getProperty(String key) 用指定的键在此属性列表中搜索属性。 
String getProperty(String key, String defaultValue) 用指定的键在属性列表中搜索属性。 
load(InputStream in)
load(Reader reader)
store(OutputStream String)

store(Writer String)

package com.zll.properties;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;
/**
	Properties演示
 */
public class PropDemo1 {
	public static void main(String[] args) throws Exception {
//		Properties prop = new Properties();
//		prop.setProperty("name", "zhangsan");
//		prop.setProperty("age", "19");
//		
//		String value = prop.getProperty("name");
//		System.out.println(value);
//		System.out.println(prop.getProperty("addr","wudangshan"));
		Reader reader = new FileReader("1.properties");
		Properties prop = new Properties();
		prop.load(reader);
		reader.close();
		
		String user = prop.getProperty("user");
		String password = prop.getProperty("password");
		System.out.println(user+":"+password);
		prop.setProperty("url", "jdbc:mysql:///day09");

		Writer writer = new FileWriter("1.properties");
		prop.store(writer, null);
		writer.close();
		System.out.println();
		
	}
}

2.打印流

Writer
PrintWriter
通过String路径
通过File对象
通过包装其他Writer
通过包装其他OutputStream


在调用 println、printf 或 format 方法时底层会自动刷新不需要我们刷新
写数据的方法没有异常抛出
提供了对各种类型支持的print方法和println方法,底层就是将传入的内容输出到流中pathSeparator

OutputStream
FilterOutputStream
PrintStream

在调用 println、printf 或 format 方法时底层会自动刷新不需要我们刷新
写数据的方法没有异常抛出

提供了对各种类型支持的print方法和println方法,底层就是将传入的内容输出到流中

3.SequenceInputStream

序列流,可以将多个字节输入流包装成一个流,相当于将流合并。

package zll.material.java_base_homework;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.util.Vector;

public class SequenceInputStreamDemo1 {
	public static void main(String[] args) throws Exception {
		InputStream in1 = new FileInputStream("e:\\1.txt");
		InputStream in2 = new FileInputStream("e:\\2.txt");
		InputStream in3 = new FileInputStream("e:\\3.txt");

		Vector v = new Vector();
		v.add(in1);
		v.add(in2);
		v.add(in3);
	
		SequenceInputStream sis = new SequenceInputStream(v.elements());
		
		OutputStream out = new FileOutputStream("e:\\4.txt");
		
		int i = -1;
		while((i = sis.read())!=-1){
			out.write(i);
		}
		
		out.close();
		sis.close();
		
	}
}

切分视屏,合并视屏

package zll.material.java_base_homework;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
/**
	切割文件
 */
public class SplitFileDemo1 {
	public static void main(String[] args) throws Exception {
		File file = new File("e:\\1.mp4");
		
		InputStream in = new FileInputStream(file);
		
		byte bs [] = new byte[1024*1024];
		
		int i = -1;
		int count = 0;
		while((i = in.read(bs))!=-1){
			count++;
			OutputStream out = new FileOutputStream("e:\\part"+count+".data");
			out.write(bs,0,i);
			out.close();
		}
		
		in.close();
		
		
		OutputStream out = new FileOutputStream("config.properties");
		Properties prop = new Properties();
		prop.setProperty("fname", "1.avi");
		prop.setProperty("count", count+"");
		prop.store(out, null);
		out.close();
	}
}
package zll.material.java_base_homework;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.util.Properties;
import java.util.Vector;

public class CombineFile {
	public static void main(String[] args) throws Exception {
		Properties prop = new Properties();
		prop.load(new FileInputStream("config.properties"));
		
		//原来文件的名字
		String fname = prop.getProperty("fname");
		//切割的块的数量
		int count = Integer.parseInt(prop.getProperty("count"));
		
		
		Vector v = new Vector();
		for(int i = 1;i<=count;i++){
			InputStream in = new FileInputStream("e:\\part"+i+".data");
			v.add(in);
		}
		
		
		SequenceInputStream sis = new SequenceInputStream(v.elements());
		OutputStream out = new FileOutputStream("F:\\"+fname);
		
		byte [] bs = new byte[1024];
		int i = -1;
		while((i = sis.read(bs))!=-1){
			out.write(bs,0,i);
		}
		
		sis.close();
		out.close();
		
	}
}

4.RandomAccessFile

随机读写文件的流。既可以读取也可以写入。在此类看来一个被操作的文件就像一个非常大的字节数组一样,并在该字节数组内部维系了一个指针,这个指针可以被设置到指定位置,从而从指定位置开始进行读写的操作。
RandomAccessFile提供了读写文件指定位置部分字节信息的能力。
RandomAccessFile(File file, String mode) 
RandomAccessFile(String name, String mode) 
 
如果文件不存在就创建该文件。如果文件已经存在就直接使用而不会覆盖。
int skipBytes(int n) //让指针跳过指定字节指向后续位置

void seek(long pos) //移动指针到指定位置,其中的参数表示从文件的开始位置计算的字节偏移量。

package com.zll.io;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 *	RandomAccessFile的使用
 *	文件随机读写流
 */
public class RandomAccessFileDemo1 {
	public static void main(String[] args) throws IOException {
//		RandomAccessFile raf = new RandomAccessFile("c:\\1.txt", "rw");
//		raf.skipBytes(27);
//		raf.write("男".getBytes());
//		raf.close();
	
		byte [] bs = new byte[4];
		RandomAccessFile raf = new RandomAccessFile("c:\\1.txt", "rw");
		raf.skipBytes(5);
		raf.read(bs);
		raf.close();
		String str = new String(bs);
		System.out.println(str);
	}
}

5.对象持久化流

ObjectOutputStream(OutputStream out) 
ObjectInputStream(InputStream in) 

利用这两个流可以将对象持久化到设备中,可以从设备中将之前持久化的对象再读取回来。
想要被序列化的对象的类必须实现Serializable接口。
静态成员变量属于类,不能被序列化
某个想要被序列化的对象类中的某个属性如果没有实现Serializable接口,则在序列化过程中,该属性不能被序列化。
如果想要手动指定某个属性不被序列化,可以使用java中的transient关键字

序列化和反序列化时,会根据serialVersionUID来判断序列化之前的类和要反序列化的列是否是同一个。
serialVersionUID默认自动取值,每次编译值都不同。

我们可以通过在类中指定private static final long serialVersionUID = 999888777l;手动指定一个serialVerwsionUID,实现即使类发生轻微变化仍然可以反序列化、实现远程对象传递时的序列化反序列化问题。

package com.zll.domain;

public class Dog {
	private String name;
	public Dog() {
	}
	
	
	
	public Dog(String name) {
		super();
		this.name = name;
	}



	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
package com.zll.domain;

import java.io.Serializable;

public class Person implements Serializable {
	private static final long serialVersionUID = 999888777l;
	private String name;
	private int age;
	private Dog dog;
	private String addr;

	public Person() {
	}
	
	public Person(String name, int age,Dog dog) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Dog getDog() {
		return dog;
	}

	public void setDog(Dog dog) {
		this.dog = dog;
	}
}
package com.zll.io;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

import com.tarena.domain.Person;
/**
 *	序列化流案例
 */
public class ObjectInputStreamDemo1 {
	public static void main(String[] args) throws Exception{
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c:\\p1.data"));
		Person p = (Person) ois.readObject();
		System.out.println(p.getName());
		System.out.println(p.getAge());
		System.out.println(p.getDog());
		System.out.println(p);
	}
}
package com.zll.io;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import com.tarena.domain.Dog;
import com.tarena.domain.Person;

/**
	序列化流
 */
public class ObjectOutputStreamDemo1 {
	public static void main(String[] args) throws Exception {
		Person p = new Person("zhangsan",19,new Dog("阿黄"));
		System.out.println(p);
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c:\\p1.data"));
		oos.writeObject(p);
		oos.close();
	}
}

6.基本数据流

ByteArrayInputStream ByteArrayOutputStream 
-- 数据来源/目的地 是内存中的字节数组的流
ByteArrayOutputStream() 
ByteArrayOutputStream(int size) 
byte[] toByteArray() //将ByteArrayOutputStream中的保存的字节转换位字节数组返回。
 
ByteArrayInputStream(byte[] buf) 

ByteArrayInputStream(byte[] buf, int offset, int length) 

package com.zll.io;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
/**
 * ByteArrayInputStream 案例
 */
public class ByteArrayInputStreamDemo1 {
	public static void main(String[] args) throws Exception {
		byte [] bs = "中国北京大钟寺".getBytes();
		
		ByteArrayInputStream in = new ByteArrayInputStream(bs);
		OutputStream out = new FileOutputStream("c:\\2.txt");
		
		int i = -1;
		while((i = in.read())!=-1){
			out.write(i);
		}
		
		in.close();
		out.close();
	}
}
package com.zll.io;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
/**
 * ByteArrayOutputStream 案例
 */
public class ByteArrayOutputStreamDemo1 {
	public static void main(String[] args) throws Exception {
		InputStream in = new FileInputStream("c:\\1.txt");
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		
		int i = -1;
		while((i=in.read())!=-1){
			bos.write(i);
		}
		
		byte [] arr = bos.toByteArray();//将ByteArrayOutputStream中的保存的字节信息转换为字节数组返回
		
		System.out.println(new String(arr));
		
		in.close();
		bos.close();
		
	}
}

        CharArrayReader CharArrayWriter

-- 数据来源/目的地 是内存中的字符数组的流
package com.zll.io;

import java.io.CharArrayReader;
import java.io.CharArrayWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
/**
 * CharArrayWriter 案例
 */
public class CharArrayWriterDemo1 {
	public static void main(String[] args) {
		CharArrayWriter writer = new CharArrayWriter();
	
		CharArrayReader reader = new CharArrayReader(new char[]{});
	}
}
StringReader StringWrtier
-- 数据来源/目的地 是字符串
package com.zll.io;

import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;
/**
 * StringReader案例
 */
public class StringReaderDemo1 {
	public static void main(String[] args) throws Exception {
		StringReader reader = new StringReader("xyzopqrstzyx");
		Writer writer = new FileWriter("c:\\3.txt");
		
		
		int i = -1;
		while((i = reader.read())!=-1){
			writer.write(i);
		}
		
		reader.close();
		writer.close();
	}
}
package com.zll.io;

import java.io.FileReader;
import java.io.Reader;
import java.io.StringWriter;
/**
 * StringWriter案例
 */
public class StringWriterDemo1 {
	public static void main(String[] args) throws Exception {
		StringWriter writer = new StringWriter();
		Reader reader = new FileReader("c:\\1.txt");
		
		int i = -1;
		while((i = reader.read())!=-1){
			writer.write(i);
		}
		
		String str = writer.toString();
		System.out.println(str);
		
		reader.close();
		writer.close();
		
	}
}





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值