Hir*_*oth 5 java file path fileoutputstream
我使用FileOutputStream创建了一个文件,它是一个excel文件(使用HSSF Liberary)
FileOutputStream fileOut = new FileOutputStream(text+".xls");
Run Code Online (Sandbox Code Playgroud)
然后我在excel文件(工作簿)中编写我需要的内容,然后关闭文件
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
Run Code Online (Sandbox Code Playgroud)
关闭后我需要向用户显示文件的路径(我知道它在我的应用程序的文件夹中创建,但我仍然需要将其显示给用户,可能通过joption/message box)
我试过这个:
String absolutePath = fileOut.getAbsolutePath();
JOptionPane.showMessageDialog(null, absolutePath);
Run Code Online (Sandbox Code Playgroud)
但它显示错误,它说它找不到方法"getAbsolutePath".我该怎么办 ?无论如何,我可以得到这条路吗?
您可以更改代码以使用文件作为中介.
File myFile = new File(text + ".xls");
FileOutputStream fileOut = new FileOutputStream(myFile);
Run Code Online (Sandbox Code Playgroud)
然后只是得到的道路:
String absolutePath = myFile.getAbsolutePath();
Run Code Online (Sandbox Code Playgroud)
确保在完成后关闭流:
fileOut.close();
Run Code Online (Sandbox Code Playgroud)
理想情况下,您不应该只在Java路径碰巧设置的地方创建文件.您应该重新考虑这一点,而是询问用户他们想要保存文件的位置.
| 归档时间: |
|
| 查看次数: |
6708 次 |
| 最近记录: |