Thursday, October 8, 2015

Copy File From Project Path to Desktop with the Creation of Directory

Below code will copy the file from the working directory and it will also create the Directory on Desktop and copy the same file in newly created directory-


import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class GenericUtility
{

    public static void copyTestCase()
    {
     
       // It will look for the user home path and will get the desired path as provided, here we have                //chosen the Desktop
        Path desktop = FileSystems.getDefault().getPath( System.getProperty("user.home") + "/Desktop");

           //new directory will be created
        File file = new File(desktop.toString() + "\\TestSuite");
        if (!file.exists())
        {
            if (file.mkdir())
            {
                System.out.println("Directory is created!");
            }
            else
            {
                System.out.println("Failed to create directory!");
            }
        }
        Path oriPath = FileSystems.getDefault().getPath("src/com/javafries/automation/properties",
                "TestCases.xlsx");
        // Path copyPath =
        // FileSystems.getDefault().getPath(System.getProperty("user.home")+"/Desktop",
        // "TestCases.xlsx");
        Path copyPath = FileSystems.getDefault().getPath(file.toString(),
                "TestCases.xlsx");
   
        if (!f.exists())
        {
            try
            {
                Files.copy(oriPath, copyPath, StandardCopyOption.COPY_ATTRIBUTES);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args)
    {
        copyTestCase();
    }
}

2 comments:

  1. How to sort data after writing to excel using Java. I have written access log data to excel in some format and I want the data to be in an order of time stamp. How to do that

    ReplyDelete
  2. How to sort data after writing to excel using Java. I have written access log data to excel in some format and I want the data to be in an order of time stamp. How to do that

    ReplyDelete