第六篇 IO流技术(六)

时间:2022-10-29 21:36:18
package com.zzp.demo01;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 
 * 四个步骤:字节数组输入流
 * 1、创建源:字节数组不能过大
 * 2、选择流
 * 3、操作
 * 4、释放资源:可以不用处理
 * @author java
 *
 */
public class IOTest07 {
    public static void main(String[] args) {
        //1、创建源
        byte[] src = "talk is cheap show me the code".getBytes();//相对路径
        //2、选择流
        InputStream is = null;
        try {
            is = new ByteArrayInputStream(src);
            //3、操作  分段读取
            byte[] flush = new byte[5]; //每次读取五个字节
            int len = -1;//设置默认长度为-1
            while((len = is.read(flush))!= -1){
                String str = new String(flush, 0, len);//将字节转化为字符串
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(null != is){
                //4、释放资源
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }         
    }
}
package com.zzp.demo01;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStream;

/**
 * 
 * 四个步骤:(字节数组输出流)
 * 1、创建源:内部维护
 * 2、选择流:不关联源
 * 3、操作
 * 4、释放资源:可以不用
 * 最后要拿一下数据使用toByteArray
 * @author java
 *
 */
public class IOTest08 {
    public static void main(String[] args) {
        //1、创建源
        byte[] dest = null;
        //2、选择流
        ByteArrayOutputStream os = null;
        try {
            os = new ByteArrayOutputStream();
            //3、操作(写入操作)
            String msg = "我爱我的国家";
            byte[] datas = msg.getBytes();//将字符串转化成字节数组
            os.write(datas, 0, datas.length);
            os.flush();//刷新
            byte[] byteArray = os.toByteArray();
            System.out.println(byteArray.length+"----"+new String(byteArray, 0, os.size()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(null != os){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

 图片的拷贝

这种方式,在字节数组转化成图片的时候,一定要注意设置字节读取的大小

package com.zzp.demo01;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStream;

/**
 * 
 * 1、图片转化成字节数组
 * 
 * 2、字节数组转化成图片
 * @author java
 *
 */
public class IOTest09 {
    public static void main(String[] args) {
        byte[] fileTobyteArray = fileTobyteArray("4.JPG");
        byteArrayTofile(fileTobyteArray, "2.JPG");
    }
    //1、图片转化成字节数组
    public static byte[] fileTobyteArray(String filePath){
        //1、创建源
        File src = new File(filePath);//相对路径
        //2、选择流
        InputStream is = null;
        ByteArrayOutputStream os = null;
        byte[] dest = null;
        try {
            is = new FileInputStream(src);
            os = new ByteArrayOutputStream();
            //3、操作  分段读取
            byte[] flush = new byte[1024*10]; //10k一读取
            int len = -1;//设置默认长度为-1
            while((len = is.read(flush))!= -1){
                os.write(flush, 0, len);
            }
            os.flush();//刷新
            dest = os.toByteArray();
            return dest;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(null != is){
                //4、释放资源
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }         
    
        return null;
    }
    
    //2、将字节数组转化成图片
    public static void byteArrayTofile(byte[] datas,String filePath){
        //1、创建源
        File dest = new File(filePath);
        //2、选择流
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new ByteArrayInputStream(datas);
            os = new FileOutputStream(dest);
            //3、操作  分段读取
            byte[] flush = new byte[1024*50]; //每次读取10k
            int len = -1;//设置默认长度为-1
            while((len = is.read(flush))!= -1){
                os.write(datas, 0,len);                
            }
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(null != is){
                //4、释放资源
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(null != os){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }         
    }
}

 

package com.zzp.demo01;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
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;

public class FileUtils {
    public static void main(String[] args) {
        //文件到文件
        try {
            InputStream is = new FileInputStream("1.txt");
            OutputStream os = new FileOutputStream("copy_1.txt");
            copy(is, os);
        } catch (IOException e) {            
            e.printStackTrace();
        }
        
        //文件到字节数组的拷贝
        byte[] datas = null;
        try {
            InputStream is = new FileInputStream("4.JPG");
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            copy(is, os);
            datas = os.toByteArray();
        } catch (IOException e) {            
            e.printStackTrace();
        }
        
        //字节数组到文件
        try {
            InputStream is = new ByteArrayInputStream(datas);
            OutputStream os = new FileOutputStream("copy_4.JPG");
            copy(is, os);
        } catch (IOException e) {            
            e.printStackTrace();
        }
    }
    
    //文件到文件的拷貝
    public static void copy(InputStream is, OutputStream os) {
        try {
            // 3、操作(写入操作)
            byte[] flush = new byte[1024]; // 1k一读取
            int len = -1;// 设置默认长度为-1
            while ((len = is.read(flush)) != -1) {
                os.write(flush, 0, len);
            }
            os.flush();// 刷新
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {}
    }
    //关闭流资源
    public static void close(InputStream is,OutputStream os){
        // 先打开的后关闭
        try {
            if (null != os) {
                os.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (null != is) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }
    
    //关闭多个流
    public static void close(Closeable... ios){
        for(Closeable io : ios){
            if (null != io) {
                try {
                    io.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
package com.zzp.demo01;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
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;

public class FileUtils2 {
    public static void main(String[] args) {
        //文件到文件
        try {
            InputStream is = new FileInputStream("1.txt");
            OutputStream os = new FileOutputStream("copy_1.txt");
            copy(is, os);
        } catch (IOException e) {            
            e.printStackTrace();
        }
        
        //文件到字节数组的拷贝
        byte[] datas = null;
        try {
            InputStream is = new FileInputStream("4.JPG");
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            copy(is, os);
            datas = os.toByteArray();
        } catch (IOException e) {            
            e.printStackTrace();
        }
        
        //字节数组到文件
        try {
            InputStream is = new ByteArrayInputStream(datas);
            OutputStream os = new FileOutputStream("copy_4.JPG");
            copy(is, os);
        } catch (IOException e) {            
            e.printStackTrace();
        }
    }
    
    //文件到文件的拷貝
    public static void copy(InputStream is, OutputStream os) {
        try(is;os) {
            // 3、操作(写入操作)
            byte[] flush = new byte[1024]; // 1k一读取
            int len = -1;// 设置默认长度为-1
            while ((len = is.read(flush)) != -1) {
                os.write(flush, 0, len);
            }
            os.flush();// 刷新
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
    /*//关闭流资源
    public static void close(InputStream is,OutputStream os){
        // 先打开的后关闭
        try {
            if (null != os) {
                os.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (null != is) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }
    
    //关闭多个流
    public static void close(Closeable... ios){
        for(Closeable io : ios){
            if (null != io) {
                try {
                    io.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }*/
}
package com.zzp.demo01;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStream;

/**
 * 
 * 四个步骤:文件拷贝(文件字节输入流和文件字节输出流) 1、创建源 2、选择流 3、操作 4、释放资源
 * 
 * @author java
 *
 */
public class Copy {
    public static void main(String[] args) {
        copy("src/com/zzp/demo01/Copy.java", "2.txt");
    }

    public static void copy(String srcPath, String destPath) {
        // 1、创建源
        File src = new File(srcPath);//
        File dest = new File(destPath);// 目的地
        // 2、选择流
        /*InputStream is = null;
        OutputStream os = null;*/
        try(InputStream is = new FileInputStream(src);
                OutputStream os = new FileOutputStream(dest, true);) {
            /*is = new FileInputStream(src);
            os = new FileOutputStream(dest, true);*/// 加true表示后面进行拼接,如果为false,则替换之前的内容
            // 3、操作(写入操作)
            byte[] flush = new byte[1024]; // 1k一读取
            int len = -1;// 设置默认长度为-1
            while ((len = is.read(flush)) != -1) {
                os.write(flush, 0, len);
            }
            os.flush();// 刷新
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } /*finally {// 先打开的后关闭
            try {
                if (null != os) {
                    os.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if (null != is) {
                    is.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }*/
    }
}