java自学之路-----PrintStream 文件分割与合并 流中一些有特点的类

PrintStream{

特点{

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

2.不抛出IOException

}


构造函数{

1.字符串路径

2.File对象

3.字节输出流

}

}


小练习:将一个文件进行分割再进行合并:{

分割方法,需要指定被分割文件对象

	/**
	 * 	  步骤
	 * 	1.创建输入流,读取文件内容
	 * 	2.创建缓冲区,
	 * 	3.将读取内容存入缓冲区
	 * 	4.创建输出流
	 * 	5.创建文件对象
	 * 	6.将输出流中的内容写入文件中
	 * 	7.关闭资源
	 */
	private static void cutFileMethod(File file) throws IOException {
		InputStream is = new FileInputStream(file);
		byte[] buf = new byte[1024*1024];
		int len = 0;
		int count = 1;		
		while((len=is.read(buf)) != -1){
			File f = new File("f:\\a\\" + count++ + ".txt");
			OutputStream os = new FileOutputStream(f);
			os.write(buf, 0 , len);
			os.close();			
		}
		//创建一个配置文件,包含文件分割的信息
		File f = new File("f:\\a\\" + count + ".properties");
		Properties p = new Properties();
		p.put("filenum", "" + --count);
		p.put("filename", "" + file.getName());
		p.store(new FileOutputStream(f), "分割文件信息");
	}

将分割后的文件进行合并


	/**
	 * 思路
	 * 	1.获取配置文件中的内容,读取文件名,以及分割后的个数
	 * 	2.读取每个部分,
	 * 	3.创建一个输出流,将读取的数据写出
	 */
	private static void joinFileMethod(File file) throws IOException, IOException {
		// TODO Auto-generated method stub
		if(!file.exists())
			throw new RuntimeException();
		File[] f = file.listFiles(new FilterTool(".properties"));
		if(f.length != 1)
			throw new RuntimeException("该目录下没有properties扩展名的文件或者不唯一");
		File f_pro = f[0];
		
		Properties p = new Properties();
		p.load(new FileInputStream(f_pro));
		String file_name = p.getProperty("filename");
		String file_num = p.getProperty("filenum");
		File[] file_part = file.listFiles(new FilterTool(".txt"));
		if(file_part.length != Integer.parseInt(file_num))
			throw new RuntimeException("文件个数与配置文件不一致");
		
		List<FileInputStream> list = new ArrayList<FileInputStream>();
		for(File f_1 : file_part){
			list.add(new FileInputStream(f_1));	
		}
		Enumeration<FileInputStream> en = Collections.enumeration(list);
		SequenceInputStream sis = new SequenceInputStream(en);
		byte[] buf = new byte[1024];
		File f_join = new File(file, file_name);
		FileOutputStream fos = new FileOutputStream(f_join);
		
		int len = 0;
		while((len =sis.read(buf)) != -1){
			fos.write(buf, 0, len);
			fos.flush();			
		}
		fos.close();
		sis.close();
	}


}


IO一些独特的流{

ObjectOutputStream{
特点:操作对象的流

ObjectOutputStream:序列化(将对象存入文件)

ObjectInputStream:反序列化(从文件中读取对象数据)

注意:
1.操作的对象必须序列化,实现Serializable接口(无需覆盖方法)
2.实现接口将对象进行排序
3.保证类与对象是同一个版本
4.可以自定义序列号

例:ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L

transient:该关键字修饰的数据不会被序列化
}

RandomAccessFile{
用法:进行随机访问文件,创建该对象需要指定文件对象,以及模式(即:“r” “rw” “rws” “rwd”)
特点:
1.该类绑定的文件存在的时候不创建,不存在就创建(普通的输出流不管文件存在不存在都创建新文件)
2.该类的对象技能进行读的操作,又能写内容到文件
3.内部有一个byte数组,通过指针可以操作数组的元素,同时实现随机访问文件
4.通过getFilePointer方法获取指针的位置,通过seek方法指定指针位置
5.该对象的本质是将字节输入流和输出流进行封装
6.该对象的源或目的只能是文件,不能使目录
}

PipedStream{
用法:管道流,能够将管道的输入输出流联通,常和线程一起使用,边读边写
练习(使用线程进行读写操作)
public class PipedStreamDemo {

	public static void main(String[] args) throws IOException {
		// 创建一个管道输入,一个管道输出
		PipedInputStream pis = new PipedInputStream();
		PipedOutputStream pos = new PipedOutputStream();
		// 将两个管道联通
		pis.connect(pos);
		// 创建线程
		// 执行两个管道的任务
		
		new Thread(new Input(pis)).start();
		new Thread(new Output(pos)).start();
	}
}

class Input implements Runnable{
	PipedInputStream pis = null;
	
	Input(PipedInputStream pis){
		this.pis = pis;	
	}

	//该管道的任务,就是进行读的操作,
	public void run() {
		// TODO Auto-generated method stub
		byte[] buf = new byte[1024];
		int len = 0;
		
		try {
			len = pis.read(buf);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(new String(buf, 0, len));
	}
}

class Output implements Runnable{
	PipedOutputStream pos = null;
	
	Output(PipedOutputStream pos){
		this.pos = pos;	
	}

	//该管道的任务,就是进行写的操作,
	public void run() {
		// TODO Auto-generated method stub
		try {
			pos.write("哈哈".getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("出错");
		}finally{
			try {
				pos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
}

DataStream{

用法:将一个流对象包装成能够自由访问基本类型数据的流,将一些信息以某种形式写入文件或者从文件中读取

注意:构造方法只有一个,必须指定一个流对象,

}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章