黑马程序员-PrintWriter-序列流-分割文件-合并文件

PrintWrite(打印流)
PrintWrite()与PrintStream()
可以直接操作流和文件。
打印流:
该流提供了打印方法,可以将各种数据类型的数据都原样打印。
字节打印流: 
PrintStream
构造函数可以接收的参数类型。
  1.file对象 File
  2.字符串路径 String
  3.字节输出流 OutputStream
字符打印流 
PrintWriter 
  1.file对象 File
  2.字符串路径 String
  3.字节输出流 OutputStream
  3.字符输出流 Write
练习代码:
import java.io.*;
	/*
	 * 打印流:
	 * 该流提供了打印方法,可以将各种数据类型的数据都原样打印。
	 * 
	 * 字节打印流:
	 * PrintStream
	 * 构造函数可以接收的参数类型。
	 * 1.file对象 File
	 * 2.字符串路径 String
	 * 3.字节输出流 OutputStream
	 * 
	 * 字符打印流
	 * PrintWriter
	 * 1.file对象 File
	 * 2.字符串路径 String
	 * 3.字节输出流 OutputStream
	 * 3.字符输出流 Write
	 */
public class PrintStreamDemo {

	public static void main(String[] args) {
	
		method_2();
		
	}
	//PrintWriter,打印在控台上
	public static void method_1(){
		BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(System.out,true);
		String line = null;
		try {
			while((line = buf.readLine()) != null){
				if(line.equals("over"))
					break;
				pw.println(line.toUpperCase());
				//pw.flush();
			}
		} catch (IOException e) {
			throw new RuntimeException("文件读写异常");
		}
	}
	//PrintWrite打印在文本文件中
	public static void method_2(){
		File file = new File("text.txt");
		BufferedReader  buf = new BufferedReader(new InputStreamReader(System.in));
		try {
			if(!file.exists())
				file.createNewFile();
			//带自动刷新的PrintWriter构造函数
			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)), true);
			String line = null;
			while((line = buf.readLine()) != null){
				if(line.equals("over"))
					break;
				pw.println(line.toUpperCase());
				//pw.flush();
			}
			pw.close();
		} catch (FileNotFoundException e) {
			throw new RuntimeException("文件找不到");
		}
		catch( IOException ex){
			throw  new RuntimeException("文件读写异常");
		}finally{
			try {
				buf.close();
			} catch (IOException e) {
				throw new RuntimeException("流关闭异常");
			}
		}
	}
}


序列流(合并文件)

有些情况下,当我们需要从多个输入流中向程序读入数据。此时,可以使用合并流,将多个输入流合并成一个SequenceInputStream流对象。

SequenceInputStream会将与之相连接的流集组合成一个输入流并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末 尾为止。 合并流的作用是将多个源合并合一个源。

public SequenceInputStream(Enumeration<? extends InputStream> e)

通过记住参数来初始化新创建的 SequenceInputStream,该参数必须是生成运行时类型为 InputStream 对象的 Enumeration 型参数。将按顺序读取由该枚举生成的输入流,以提供从此 SequenceInputStream 读取的字节。在用尽枚举中的每个输入流之后,将通过调用该流的 close 方法将其关闭。

public interface Enumeration<E>

实现 Enumeration 接口的对象,它生成一系列元素,一次生成一个。连续调用 nextElement 方法将返回一系列的连续元素。

练习代码:

import java.io.*;
public class SequenceDemo {
	
	public static void main(String[] args) {
		Vector<FileInputStream> v = new Vector<FileInputStream>();
		try {
			v.add(new FileInputStream("1.txt"));
			v.add(new FileInputStream("2.txt"));
			v.add(new FileInputStream("3.txt"));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		
		Enumeration<FileInputStream> e = v.elements();
		SequenceInputStream sis = new SequenceInputStream(e);
		FileOutputStream fouts = null;
		try {
			fouts = new FileOutputStream("SequenceTest.txt");
			byte[] value = new byte[1024];
			int len = 0;
			while((len = sis.read(value)) != -1){
				fouts.write(value,0,len);
			}
		} catch (FileNotFoundException e1) {
			throw new RuntimeException("合并流创建失败");
		}catch(IOException e2){
			throw new RuntimeException("合并流读写失败");
		}finally{
			try {
				sis.close();
			} catch (IOException e2) {
				throw new RuntimeException("合并流关闭异常");
			}
			if(fouts != null){
				try {
					fouts.close();
				} catch (IOException e1) {
					throw new RuntimeException("文件写入流关闭异常");
				}
			}
		}
	}
}

切割文件
切割后的文件后缀名是.part(碎片文件)
当一个方法中的匿名类或者局部内部类需要访问方法区域中的变量时,该变量参数必须定义为fina。
追究根本原因其实就是作用域中变量的生命周期导致的。 
首先,内部类和外部类其实是处于同一个级别,内部类不会因为定义在方法中就会随着方法的执行完毕而跟随者被销毁。问题就来了,如果外部类的方法中的变量不定义final,那么当外部类方法执行完毕的时候,这个局部变量肯定也就被GC了,然而内部类的某个方法还没有执行完,这个时候他所引用的外部变量已经找不到了。如果定义为final,java会将这个变量复制一份作为成员变量内置于内部类中,这样的话,由于final所修饰的值始终无法改变,所以这个变量所指向的内存区域就不会变。
练习代码:
import java.io.*;

public class SplitFile {

	public static void main(String[] args) {
		split();
		merger();
	}
	//合并文件
	public static void merger(){
		ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
			try {
				for(int i = 1; i <= 7; i++){
				al.add(new FileInputStream("test"+ i +".part"));
			}
			} catch (FileNotFoundException e) {
				throw new RuntimeException("文件路径获取异常");
			}
			final Iterator<FileInputStream> is = al.iterator();
			Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() {

				@Override
				public boolean hasMoreElements() {
					return is.hasNext();
				}

				@Override
				public FileInputStream nextElement() {
					return is.next();
				}
				
			};
			SequenceInputStream sis = new SequenceInputStream(en);
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream("Target.bmp");
				byte[] value = new byte[1024*1024];
				int len = 0;
				while((len = sis.read(value)) != -1){
					fos.write(value,0,len);
				}
			} catch (FileNotFoundException e) {
				throw new RuntimeException("文件输出流异常");
			} catch(IOException e1){
				throw new RuntimeException("文件读取异常");
			}finally{
				try {
					sis.close();
				} catch (IOException e1) {
					throw new RuntimeException("序列流关闭异常");
				}
				if(fos != null){
					try {
						fos.close();
					} catch (IOException e) {
						throw new RuntimeException("文件写入流关闭异常");
					}
				}
			}
	}
	//分割文件
	public static void split(){
		FileInputStream fins = null;
		FileOutputStream fouts = null;
		try {
			fins = new FileInputStream("test.bmp");
			byte[] value = new byte[1024*1024];
			int count = 1;
			int len = 0;
			while((len = fins.read(value)) != -1){
				fouts = new FileOutputStream("test" + count++ +".part");
				fouts.write(value,0,len);
				fouts.close();
			}
		} catch (Exception e) {
			throw new RuntimeException("文件读写流异常");
		}finally{
			if(fins != null){
				try {
					fins.close();
				} catch (IOException e) {
					throw new RuntimeException("文件读取流关闭异常");
				}
			}
		}
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值