The Project need is to copy the excel file from source to destination folder which is created based on the current date. I am able to create the current date & time folder but not able to copy the file there. Getting error message as "Access Denied"
public static void writeRequestAndResponse() throws IOException {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String currentDateTime = format.format(date);
String folderPath = "E:\\QA\\Output\\" + currentDateTime ;
File theDir = new File(folderPath);
// if the directory does not exist, create it
if (!theDir.exists()) {
System.out.println("creating directory: " + theDir.getName());
boolean result = false;
try {
theDir.mkdirs();
result = true;
final String folderpath1 = folderPath + "\\test.api\\" + "\\exceloutput";
File theDir1 = new File(folderpath1);
theDir1.mkdirs();
System.out.println(folderpath1);
String frompath = "E:\\Project\\src\\main\\java\\com\\qa\\testdata\\APITestData.xlsx";
//FileInputStream is = new FileInputStream(frompath);
File file1 = new File(frompath);
//String str1="E:\\QA\\Output\\20200121172737\\tc.api\\exceloutput";
// File file2 = new File(str1);
final String topath=folderpath1;
File file2 = new File(topath);
//PrintWriter out = new PrintWriter(new FileOutputStream(topath));
//FileOutputStream outfs= new FileOutputStream(topath);
Files.copy(file1,file2);
}
catch (SecurityException se) {
// handle it
System.out.println(se.getMessage());
}
if (result) {
System.out.println("Folder created");
}
} else if (theDir.exists()) {
System.out.println("Folder exist");
}
}
Error message display in Console as "java.io.FileNotFoundException: E:\QA\Output\20200122094149\test.api\exceloutput (Access is denied)"
I couldn't figure out the version of Java you are using. Hence, I used Java 8. Then with these 2 changes, it is working.
Files.copy()
takes Path
instances in Java 8 instead of File
instances. So, changed that.Files.copy()
, the destination Path
should point to a file and not folder.Changed code (with commented code removed):
public static void writeRequestAndResponse(){
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat( "yyyyMMddHHmmss" );
String currentDateTime = format.format( date );
String folderPath = "E:\\QA\\Output\\" + currentDateTime;
File theDir = new File( folderPath );
// if the directory does not exist, create it
if (!theDir.exists()) {
System.out.println( "creating directory: " + theDir.getName() );
boolean result = false;
try {
theDir.mkdirs();
result = true;
final String folderpath1 = folderPath + "\\test.api\\" + "\\exceloutput";
File theDir1 = new File( folderpath1 );
theDir1.mkdirs();
System.out.println( folderpath1 );
String frompath = "E:\\Project\\src\\main\\java\\com\\qa\\testdata\\APITestData.xlsx";
File file1 = new File( frompath );
final String topath = folderpath1 + "\\outputFile.xlsx"; //The output file name included
File file2 = new File( topath );
Files.copy( file1.toPath(), file2.toPath() );
} catch (Exception se) {
// handle it
System.out.println( se.getMessage() );
}
if (result) {
System.out.println( "Folder created" );
}
} else if (theDir.exists()) {
System.out.println( "Folder exist" );
}
}