美文网首页
Io流文件常用工具汇总

Io流文件常用工具汇总

作者: Yluozi | 来源:发表于2023-10-04 16:50 被阅读0次

1.将流文件转成string

(此方法在再转io时存在解析问题,建议使用FileUtils.readFileToString(file)及平替方法)
import com.alibaba.cloud.commons.io.FileUtils;

    /*将流文件转成string*/
    public static String convertInFtoS(InputStream inputStream) throws IOException {
        // 创建FileInputStream对象
        StringBuilder stringBuilder = new StringBuilder();
        try {
            // 读取文件数据
            int data = 0 ;
            while ((data = inputStream.read()) != -1) {
                System.out.print((char) data);
                stringBuilder.append(data);
            }
            // 关闭输入流
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

2.将流文件转换成存放文件

    /*将流文件转换成存放文件*/
    public static FileOutputStream  convertInFtoOutF(InputStream inputStream,String path){
        log.info("将流文件转换成jar文件....");
        try {
            FileOutputStream outputStream = new FileOutputStream(path);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                // 读取文件流到缓冲区
                outputStream.write(buffer, 0, length);
            }
            inputStream.close();
            outputStream.close();
            return  outputStream;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

3.将InputStream转换为FileInputStream文件

    /*将InputStream转换为FileInputStream文件*/
    public static FileInputStream convertToFileInputStream(InputStream inputStream) throws IOException {
        File tempFile = File.createTempFile("temp", ".tmp");
        tempFile.deleteOnExit();

        try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }
        return new FileInputStream(tempFile);
    }


4.tar 解压缩

    /*tar 解压缩*/
    public static FileInputStream extractTar(FileInputStream fileInputStream, String destinationFolderPath) throws IOException {
        File outputFile = File.createTempFile("temp", ".tmp");
        try (
                /*压缩包文件目录  String tarFilePath*/
//       FileInputStream fis = new FileInputStream(tarFilePath);
//       BufferedInputStream bis = new BufferedInputStream(fis);
                /*压缩包流文件  InputStream inputStream*/
         BufferedInputStream bis = new BufferedInputStream(fileInputStream);
         TarArchiveInputStream tarInputStream = new TarArchiveInputStream(bis)) {

            TarArchiveEntry tarEntry;
            while ((tarEntry = tarInputStream.getNextTarEntry()) != null) {

//              File outputFile = new File(destinationFolderPath, tarEntry.getName());
                outputFile.deleteOnExit();

                if (tarEntry.isDirectory()) {
                    outputFile.mkdirs();
                } else {
                    outputFile.getParentFile().mkdirs();
                    try (OutputStream outputStream = new FileOutputStream(outputFile)) {
                        byte[] buffer = new byte[4096];
                        int bytesRead;
                        while ((bytesRead = tarInputStream.read(buffer)) != -1) {
                            outputStream.write(buffer, 0, bytesRead);
                        }
                    }
                }
            }
            System.out.println("Tar file extraction completed.");
            return new FileInputStream(outputFile.getPath());

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Tar file  is  null.");
        return null;
//      File[] files = outputFile.listFiles();//获取所有子文件夹
//      if (null == files || files.length == 0) {
//          System.out.println("文件夹是空的!");
//          return null;
//      }else{
//          return new FileInputStream(files[0]);
//      }
    }

main 方法


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import com.alibaba.cloud.commons.io.FileUtils;
import com.alibaba.fastjson.JSON;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import static com.zznode.jiake.e2e.util.FileUtil.writeTxtFile;

    public static void main(String[] args) {
        String tarFile = "F:\\work\\引入\\infer_result.tar";
        String destDir = "F:\\work\\引入";
        try {
            FileInputStream fileInputStream = new FileInputStream(tarFile);
            /*1 直接拿去本地路径下的jar解压并读取csv文件*/
            FileInputStream fileInputStreamNew =extractTar(convertToFileInputStream(fileInputStream), destDir);
//          doProcessNew(fileInputStreamNew);

            /*2(模拟对端传的string)将本地路径下的文件读取转成流文件的string 再转回,再读取*/
            File file = new File(tarFile);
            String ss =  FileUtils.readFileToString(file);
//          String ss = convertInFtoS(inputStream);
//          System.out.println("");
//          System.out.println("ss resoult====");
//          System.out.println(ss);
//          InputStream inputStreamNew = new ByteArrayInputStream(ss.getBytes(StandardCharsets.UTF_8));
//          extractTar(convertToFileInputStream(inputStreamNew), destDir);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

相关文章

  • java基础-day20-IO流和StringBuffer

    IO流和StringBuffer 1. IO流 1.1 IO流概述 1.2 IO流分类 1.3 文件操作输入输出字...

  • 文件IO流

    从控制台读取输出字符从文件中写入读取字符从文件中写入读取字节文件夹操作 1. 从控制台读取输出字符 输入由Syst...

  • Java 流(Stream)、文件(File)和IO

    Java 流(Stream)、文件(File)和IO 简介 1.什么是流,什么是文件,什么是IO? 一个流可以理解...

  • java_io

    java.io.File类的使用IO原理及流的分类  |---文件流   操作对象是文件     FileInp...

  • Java的IO流

    一、IO流概述 IO流:Java对数据的操作是通过流的方式,IO流用来处理设备之间的数据传输,上传文件和下载文件,...

  • JavaSE进阶九 IO流一

    1,IO流的概述 IO流,什么是IO? I:Input O:Output 通过IO可以完成硬盘文件的读和写。...

  • 19-io_文件权限掩码_动态库_静态库

    I/O * 标准IO:库 带缓存 通过流FILE * 操作文件 * 文件IO...

  • Day 16

    文件File 路径 IO流 流中的异常处理

  • 【JavaSE(十三)】JavaIO流(中)

    1 IO流 1.1 IO流概述 Java中使用 IO流 来读取和写入,读写设备上的数据、硬盘文件、内存、键盘等等,...

  • JAVA API-day07

    A 基本的IO操作 文件流 缓冲流 节点流和处理流 对象流

网友评论

      本文标题:Io流文件常用工具汇总

      本文链接:https://www.haomeiwen.com/subject/ygbybdtx.html