第九篇 IO流技术(九)

时间:2020-12-17 21:36:11

打印流

package com.zzp.demo;

import java.awt.image.FilteredImageSource;
import java.io.BufferedOutputStream;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

/**
 * 
 * PrintStream
 * @author java
 *
 */
public class PrintTest01 {
    public static void main(String[] args) throws FileNotFoundException {
        PrintStream ps = System.out;
        ps.print("生活不易");
        ps.println("且行且珍惜");
        
        //加上true之后,自动刷新
        ps = new PrintStream(new BufferedOutputStream(new FileOutputStream("4.txt")),true);
        ps.print("生活不易");
        ps.println("且行且珍惜");
        ps.println("真是这么简单");
        ps.close();
        
        //重定向输出端
        System.setOut(ps);
        System.out.println("重定向");
        
        //重定向回控制台
        System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true));
        System.out.println("重定向回控制台");
    }
}

 PrintWriter

package com.zzp.demo;

import java.awt.image.FilteredImageSource;
import java.io.BufferedOutputStream;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * 
 * PrintWriter
 * @author java
 *
 */
public class PrintTest02 {
    public static void main(String[] args) throws FileNotFoundException {
        //加上true之后,自动刷新
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream("4.txt")),true);
        pw.print("生活不易");
        pw.println("且行且珍惜");
        pw.println("真是这么简单");
        pw.close();
    }
}

 随机读取流 RandomAccessFile

package com.zzp.demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 
 * 随机读取流 RandomAccessFile
 * 
 * @author java
 *
 */
public class RandTest01 {

    public static void main(String[] args) throws IOException {
        //分多少块
        File src = new File("src/com/zzp/demo/CopyTxt.java");
        //总长度
        long len = src.length();
        //每块的大小
        int blockSize = 1024;
        //多少块,不够整数的向上取整
        int size =(int) Math.ceil(len*1.0/blockSize);
        System.out.println(size);
        
        //起始位置
        int beginPos = 0;
        //实际大小
        int actualSize = (int)(blockSize > len?len:blockSize);
        for(int i=0;i<size;i++){
            beginPos = i*blockSize;
            if(i==size-1){//最后一块
                actualSize = (int)len;
            }else{
                actualSize = blockSize;
                len -= actualSize;
            }
            split(i,beginPos,actualSize);
        }
    }
    
    //指定起始位置,读取剩余的所有内容
    public static void test01(){
        try {
            RandomAccessFile raf = new RandomAccessFile(new File("src/com/zzp/demo/CopyTxt.java"), "r");
            //随机读取的位置
            raf.seek(2);
            // 3、操作(写入操作)
            byte[] flush = new byte[1024]; // 1k一读取
            int len = -1;// 设置默认长度为-1
            while ((len = raf.read(flush)) != -1) {
                System.out.println(new String(flush, 0, len));
            }
            raf.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    //分块思想
    public static void test02(){
        try {
            RandomAccessFile raf = new RandomAccessFile(new File("src/com/zzp/demo/CopyTxt.java"), "r");
            //起始位置
            int beginPos = 2;
            //实际大小
            int actualSize = 1026;
            //随机读取的位置
            raf.seek(beginPos);
            // 3、操作(写入操作)
            byte[] flush = new byte[1024]; // 1k一读取
            int len = -1;// 设置默认长度为-1
            while ((len = raf.read(flush)) != -1) {
                if(actualSize > len){
                    System.out.println(new String(flush, 0, len));
                    actualSize -= len;
                }else{
                    System.out.println(new String(flush, 0, actualSize));
                    break;
                }
            }
            raf.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 指定第i块的起始位置 和实际长度
     * @param i
     * @param beginPos
     * @param actualSize
     * @throws IOException
     */
    public static void split(int i,int beginPos,int actualSize ) throws IOException {
        RandomAccessFile raf =new RandomAccessFile(new File("src/com/sxt/io/Copy.java"),"r");
        //随机读取 
        raf.seek(beginPos);
        //读取
        //3、操作 (分段读取)
        byte[] flush = new byte[1024]; //缓冲容器
        int len = -1; //接收长度
        while((len=raf.read(flush))!=-1) {            
            if(actualSize>len) { //获取本次读取的所有内容
                System.out.println(new String(flush,0,len));
                actualSize -=len;
            }else { 
                System.out.println(new String(flush,0,actualSize));
                break;
            }
        }            
        
        raf.close();
    }
}

 

package com.zzp.demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 
 * 随机读取流 RandomAccessFile
 * 
 * @author java
 *
 */
public class RandTest02 {

    public static void main(String[] args) throws IOException {
        // 分多少块
        File src = new File("4.JPG");
        // 总长度
        long len = src.length();
        // 每块的大小
        int blockSize = 1024;
        // 多少块,不够整数的向上取整
        int size = (int) Math.ceil(len * 1.0 / blockSize);
        System.out.println(size);

        // 起始位置
        int beginPos = 0;
        // 实际大小
        int actualSize = (int) (blockSize > len ? len : blockSize);
        for (int i = 0; i < size; i++) {
            beginPos = i * blockSize;
            if (i == size - 1) {// 最后一块
                actualSize = (int) len;
            } else {
                actualSize = blockSize;
                len -= actualSize;
            }
            split(i, beginPos, actualSize);
        }
    }
    /**
     * 指定第i块的起始位置 和实际长度
     * 
     * @param i
     * @param beginPos
     * @param actualSize
     * @throws IOException
     */
    public static void split(int i, int beginPos, int actualSize) throws IOException {
        RandomAccessFile raf = new RandomAccessFile(new File("4.JPG"), "r");
        RandomAccessFile raf2 = new RandomAccessFile(new File("dest/"+i+"4.JPG"), "rw");
        // 随机读取
        raf.seek(beginPos);
        // 读取
        // 3、操作 (分段读取)
        byte[] flush = new byte[1024]; // 缓冲容器
        int len = -1; // 接收长度
        while ((len = raf.read(flush)) != -1) {
            if (actualSize > len) { // 获取本次读取的所有内容
                raf2.write(flush, 0, len);
                actualSize -= len;
            } else {
                raf2.write(flush, 0, actualSize);
                break;
            }
        }
        raf2.close();
        raf.close();
    }
}

 

package com.zzp.demo;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * 
 * 面向对象思想封装 分割
 * 
 * @author java
 *
 */
public class SplitFile {
    //源头
    private File src;
    //目的地(文件)
    private String destDir;
    //所有分割后的文件存储路径
    private List<String> destPaths;
    //每块的大小
    private int blockSize;
    //块数:多少块
    private int size;
    
    public SplitFile(String srcPath,String destDir){
        this(srcPath,destDir,1024);
    }
    
    public SplitFile(String srcPath,String destDir,int blockSize){
        this.destDir = destDir;
        this.src = new File(srcPath);
        this.blockSize =blockSize;
        this.destPaths =new ArrayList<String>();
        //初始化
        init();
    }
    
    //初始化
    public void init(){
        //总长度
        long len = this.src.length();        
        //块数: 多少块
        this.size =(int) Math.ceil(len*1.0/blockSize);
        //路径
        for(int i=0;i<size;i++) {
            this.destPaths.add(this.destDir +"/"+i+"-"+this.src.getName());
        }
    }
    
    /**
     * 分割
     * 1、计算每一块的起始位置及大小
     * 2、分割
     * @throws IOException 
     */
    public void split() throws IOException {
        //总长度
        long len = src.length();        
        //起始位置和实际大小
        int beginPos = 0;
        int actualSize = (int)(blockSize>len?len:blockSize); 
        for(int i=0;i<size;i++) {
            beginPos = i*blockSize;
            if(i==size-1) { //最后一块
                actualSize = (int)len;
            }else {
                actualSize = blockSize;
                len -=actualSize; //剩余量
            }
            splitDetail(i,beginPos,actualSize);
        }
    }    
    /**
     * 指定第i块的起始位置 和实际长度
     * @param i
     * @param beginPos
     * @param actualSize
     * @throws IOException
     */
    private  void splitDetail(int i,int beginPos,int actualSize ) throws IOException {
        RandomAccessFile raf =new RandomAccessFile(this.src,"r");
        RandomAccessFile raf2 =new RandomAccessFile(this.destPaths.get(i),"rw");
        //随机读取 
        raf.seek(beginPos);
        //读取
        //3、操作 (分段读取)
        byte[] flush = new byte[1024]; //缓冲容器
        int len = -1; //接收长度
        while((len=raf.read(flush))!=-1) {            
            if(actualSize>len) { //获取本次读取的所有内容
                raf2.write(flush, 0, len);
                actualSize -=len;
            }else { 
                raf2.write(flush, 0, actualSize);
                break;
            }
        }            
        raf2.close();
        raf.close();
    }
    /**
     * 指定第i块的起始位置 和实际长度
     * 
     * @param i
     * @param beginPos
     * @param actualSize
     * @throws IOException
     */
    public static void split(int i, int beginPos, int actualSize) throws IOException {
        RandomAccessFile raf = new RandomAccessFile(new File("4.JPG"), "r");
        RandomAccessFile raf2 = new RandomAccessFile(new File("dest/"+i+"4.JPG"), "rw");
        // 随机读取
        raf.seek(beginPos);
        // 读取
        // 3、操作 (分段读取)
        byte[] flush = new byte[1024]; // 缓冲容器
        int len = -1; // 接收长度
        while ((len = raf.read(flush)) != -1) {
            if (actualSize > len) { // 获取本次读取的所有内容
                raf2.write(flush, 0, len);
                actualSize -= len;
            } else {
                raf2.write(flush, 0, actualSize);
                break;
            }
        }
        raf2.close();
        raf.close();
    }
    /**
     * 文件的合并
     * @throws IOException 
     */
    public void merge(String destPath) throws IOException {
        //输出流
        OutputStream os =new BufferedOutputStream( new FileOutputStream(destPath,true));    
        Vector<InputStream> vi=new Vector<InputStream>();
        SequenceInputStream sis =null;
        //输入流
        for(int i=0;i<destPaths.size();i++) {
            vi.add(new BufferedInputStream(new FileInputStream(destPaths.get(i))));                                            
        }
        sis =new SequenceInputStream(vi.elements());
        //拷贝
        //3、操作 (分段读取)
        byte[] flush = new byte[1024]; //缓冲容器
        int len = -1; //接收长度
        while((len=sis.read(flush))!=-1) {
            os.write(flush,0,len); //分段写出
        }            
        os.flush();    
        sis.close();
        os.close();
    }
    
    public static void main(String[] args) throws IOException {
        SplitFile sf = new SplitFile("4.JPG","dest");
        sf.split();
        sf.merge("5.JPG");
    }
}