JAVA io流笔记07 字节数组流

package FileText;

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

//字节数组流
//输入流操作和文件输入流一致,但是读取的时字节数组
//输出流和文件输出流有所不同,因为有新增方法,不能使用多态

//文件-(文件输入流)-》程序-(字节数组输出流)-》字节数组
//字节数组-(字节数组输入流)-》程序-(文件输出流)-》文件
public class ByteArray07 {
public static void main(String args[]){
    read(write());
    FileToByte("D:/text/dd/Buftext.txt");
}
//输入流操作和文件输入流一致,但是读取的时字节数组
public static void read(byte[] byt){
    //数据源   //把这里当作其他电脑上的文件!!!!!!!!
//    String str = "秉忠贞之志,守谦退之节";
//    byte[] byt = str.getBytes();
    //选择流
    InputStream input = new BufferedInputStream(new ByteArrayInputStream(byt));
    //读入
    int len =0;
    byte[] byte2 = new byte[1024];
    try {
        while(-1!=(len=input.read(byte2))){
            System.out.println(new String(byte2,0,len));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

//输出流和文件输出流有所不同,因为有新增方法,不能使用多态
public static byte[] write(){
    //目的地
    byte [] byt ;
    //选择流 ,因为有新增方法,所以没有多态
    ByteArrayOutputStream byta = new ByteArrayOutputStream();
    //写出操作
    String str = "秉忠贞之志,守谦退之节";
    byte byt2[]=str.getBytes();
    byta.write(byt2, 0, byt2.length);
    //获取数据(新增的方法)
    byt=byta.toByteArray();
    try {
        byta.close();
        
    } catch (IOException e) {
        e.printStackTrace();
    }
    return byt;
}


//实现
//文件-(文件输入流)-》程序-(字节数组输出流)-》字节数组
//字节数组-(字节数组输入流)-》程序-(文件输出流)-》文件

public static byte[] FileToByte(String srcPath){//文件-(文件输入流)-》程序-(字节数组输出流)-》字节数组
    //创建文件源
    File file = new File(srcPath);
    //创建目的地
    byte[] way=null;
    try {
        //创建流
        InputStream fileinput =new BufferedInputStream( new FileInputStream(file));
        ByteArrayOutputStream Boutput = new ByteArrayOutputStream();
        int len = 0;
        byte[] byt = new byte[1024];
        while(-1!=(len=fileinput.read(byt))){
            Boutput.write(byt, 0, len);
            Boutput.flush();
            //获取数据
            way=Boutput.toByteArray();
        
        }
    } catch (IOException e) {
        System.out.println("sddsdsd");
        e.printStackTrace();
    }
    ByteToFile(way);
    return way;
}

public static void ByteToFile(byte[] byt){//字节数组-(字节数组输入流)-》程序-(文件输出流)-》文件
    //转换出的文件
    File file = new File("D:/text/dd/turnFile.txt");
    int len = 0;
    byte[] newbyt = new byte[1024];
    //建立流
    InputStream byteInput = new BufferedInputStream(new ByteArrayInputStream(byt));
    try {
        OutputStream fileOutput = new BufferedOutputStream(new FileOutputStream(file));
        while(-1!=(len=byteInput.read(newbyt))){
            fileOutput.write(newbyt, 0, len);
            fileOutput.flush();
            fileOutput.close();
        }
    } catch (IOException e) {

        e.printStackTrace();
    }
}
}

猜你喜欢

转载自blog.csdn.net/qq_40302611/article/details/85204652