首页 > 解决方案 > 如何在我的方法中获取已经存在的文件(在我的计算机上)并将其转换为 InputStream?

问题描述

我在 D:/EXPORT_BASE/Export_report 中有一个生成的文件。我需要做的是使用 filePath 字符串从本地获取此文件,然后将其转换为 InputStream。

String filePath = D:/EXPORT_BASE/Export_report/1557834965979_report.txt

我需要使用字符串来获取文件并将其写入 InputStream。

标签: java

解决方案


        File file = new File("C:/myTest.txt");
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(file);

            System.out.println("Total file size to read (in bytes) : "
                    + fis.available());

            int content;
            while ((content = fis.read()) != -1) {
                // convert to char and display it
                System.out.print((char) content);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

推荐阅读