-4

I've a generated file in D:/EXPORT_BASE/Export_report. What I need to do is use the filePath string to fetch this file from my local, and then convert this to InputStream.

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

I need to use the String to get the file and write it to InputStream.

Akhil Ranjan
  • 63
  • 2
  • 11
  • Before to post questions, you should try to look for the answer at least in Google, this only needs to know a little bit from java.io. https://docs.oracle.com/javase/8/docs/api/java/io/package-summary.html – Daniel Campos Olivares May 14 '19 at 12:40
  • A *little* bit of research would have pointed you at the answer, e.g. [FileInputStream](https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html) – stridecolossus May 14 '19 at 12:41

2 Answers2

0

Basically, like this:

public InputStream getInputStreamFromFilepath(String filepath) throws FileNotFoundException {
    File fileToOpen = new File(filepath);
    return new FileInputStream(fileToOpen);
}
Daniel Campos Olivares
  • 2,262
  • 1
  • 10
  • 17
0
        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();
            }
        }
Yiao SUN
  • 908
  • 1
  • 8
  • 26