Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find the path of file created using FileOutputStream

I created a file using FileOutputStream and it is an excel file (using HSSF Liberary)

FileOutputStream fileOut = new FileOutputStream(text+".xls");

then I write what I need in my excel file (workbook) and then close the file

workbook.write(fileOut);
fileOut.flush();
fileOut.close();

After closing it I need to display the path of the file to user, (I know that it creates in the folder of my application but I still need to display it to user, maybe via joption/message box)

I tried this :

String absolutePath = fileOut.getAbsolutePath();
JOptionPane.showMessageDialog(null, absolutePath);

but it shows error and it says that it cannot find the method "getAbsolutePath". what should I do ? is there anyway that I can get this path ?

like image 749
Hirad Gorgoroth Avatar asked Sep 20 '15 08:09

Hirad Gorgoroth


People also ask

What is a fileoutputstream?

A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time.

How to write data to a file in Python using fileoutputstream?

Example: FileOutputStream to write data to a File. In the above example, we have created a file output stream named output. The file output stream is linked with the file output.txt. To write data to the file, we have used the write () method. Here, when we run the program, the output.txt file is filled with the following content.

How to create a FileInputStream in Java?

Create a FileInputStream 1 Using the path to file#N#FileInputStream input = new FileInputStream (stringPath);#N#Here, we have created an input... 2 Using an object of the file More ...

How do I write data to the file output stream?

The file output stream is linked with the file output.txt. To write data to the file, we have used the write () method. Here, when we run the program, the output.txt file is filled with the following content. This is a line of text inside the file. Note: The getBytes () method used in the program converts a string into an array of bytes.


2 Answers

You can change your code to use a file instead as an intermediary.

File myFile = new File(text + ".xls");
FileOutputStream fileOut = new FileOutputStream(myFile);

And then just get the path of that:

String absolutePath = myFile.getAbsolutePath();

Make sure to close the stream when you're done:

fileOut.close();

Ideally though, you shouldn't just create the file wherever the Java path happens to be set. You should probably rethink this and instead ask the user where they want to save the file.

like image 98
Ownaginatious Avatar answered Sep 29 '22 06:09

Ownaginatious


Use new File(text+".xls").getAbsolutePath(). The FileOutputStream doesn't allow accessing the underlying File.

You should get into the habit of reading the javadoc instead of trying random methods. You'll then see what methods exists and what methods don't.

like image 45
JB Nizet Avatar answered Sep 29 '22 06:09

JB Nizet