How to get file object from fileoutputstream after saving a workbook

@amjad.sahi
Am trying to save the file like this in a method. How can I return as a file after the fileoutputstream is used? Any suggestions?


ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
if (fname.endsWith(“.xls”) || fname.endsWith(“.XLS”)) {
workbook.save(outputStream, com.aspose.cells.SaveFormat.EXCEL_97_TO_2003);
} else {
workbook.save(outputStream, com.aspose.cells.SaveFormat.XLSX);
}

			FileOutputStream fos1 = new FileOutputStream(fileDir + File.separator + fname);
			fos1.write(outputStream.toByteArray());
			fos1.close();

After this process, I am trying to rename the file in another process, there rename is failing(using java renameTo method) because file used by another process it says.
Had tried using workbook dispose also in finally clause. So was thinking of returning as a file and try.
Any suggestions?

@sabkan
By testing on the latest version v24.7 using the following sample code, the program can run normally without any exceptions. Please refer to the attachment. result.zip (13.2 KB)

The sample code as follows:

String fname = "a.xlsx";
Workbook workbook = new Workbook(filePath + fname);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
if (fname.endsWith(".xls") || fname.endsWith(".XLS"))
{
	workbook.save(outputStream, com.aspose.cells.SaveFormat.EXCEL_97_TO_2003);
} else {
	workbook.save(outputStream, com.aspose.cells.SaveFormat.XLSX);
}

String newFileName = "b.xlsx";
FileOutputStream fos1 = new FileOutputStream(filePath + File.separator + newFileName);
fos1.write(outputStream.toByteArray());
fos1.close();
outputStream.close();

If you still have questions, please provide sample file and runnable test code, and we will check them soon.

@John.He

Thanks for the prompt response.

@sabkan
You are welcome. If you have any questions, please feel free to contact us at any time.