0

Currently I am using Apache POI to write output in Excel in my Java code here, I am saving this output Excel file somewhere in my local drive. Now challenge is, I have to save/create this same Excel on Unix server in some directory due to some requirement.
Can anyone suggest how to write code for this?
How to set this Unix server directory path in Java code?

String excelFileName = "D://TestResult_Output_Excel.xlsx";//name of excel file
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
priya
  • 45
  • 8

1 Answers1

1

Save both the windows and unix destinations being sure to save these with a path separator on the end (/ for unix or \\ for windows)

String fileName = "TestResult_Output_Excel.xlsx"
String windowsFilePath = "D:\\outputfolder\\"
String unixFilePath = "/home/foo/folder/"

Get the operating system name where the program is running

String OS = System.getProperty("os.name");

Compare that to your OS options (Windows/UNIX in this case)

String outputFilePath;
if (OS.toUpperCase().contains("WINDOWS")) {
    outputFilePath = windowsFilePath;
} else {
    outputFilePath = unixFilePath;
}

If you need the file name to be dynamic you can add it at this point, or you could have added it to the original path variables.

outputFilePath += fileName;
Lucas Burns
  • 393
  • 1
  • 3
  • 14
  • 1
    System.getProperty can get you properties like "file.separator" ( '\' or '/'), "user.home" (home directory), "user.dir" (working dir). So you can construct a file path with these (for instance). – PJ Fanning Apr 05 '19 at 16:15