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 ?
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.
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.
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 ...
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With