import java.io.BufferedInputStream;
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 com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test2 {
    public static void main(String[] args) throws Exception {
    	
    	BufferedInputStream in = new BufferedInputStream(new FileInputStream("D:/2022-01-27.txt"));
    	 byte[] str_byte= read("D:/2022-01-28.txt");
    	 System.out.println("**************str_byte"+str_byte);
    	 
    	 String s = new String(str_byte);
    	 System.out.println("**************str_byte"+s.substring(0,20));
    	 byte[] target = null;
         ObjectMapper mapper = new ObjectMapper();
    	 target = mapper.readValue(s, new TypeReference<byte[]>() {
         });
    	saveFile("测试版.pdf",target);
    	System.out.println("**************");
    }


    /*
     *读取txt文本数据  
     *
     */
    public static byte[] read(String fileName) throws IOException {
        InputStream is=new FileInputStream(fileName);
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        byte[] buffer=new byte[is.available()];
        int n=0;
        while((n=is.read(buffer))!=-1)
        bos.write(buffer,0,n);
        bos.close();
        is.close();
        return bos.toByteArray();
     }


    /**
     * 将字节流转换成文件
     *
     * @param filename
     * @param data
     * @throws Exception
     */
    public static void saveFile(String filename, byte[] data) throws Exception {
        if (data != null) {
            String filepath = "D:\\" + filename;
            File file = new File(filepath);
            if (file.exists()) {
                file.delete();
            }
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(data, 0, data.length);
            fos.flush();
            fos.close();
        }
    }
}

  

posted on 2022-01-27 19:05  介先生不介意你  阅读(116)  评论(0)    收藏  举报