title: Java: Overwrite the existing file on server with modified file(excel sheet) using File API
body: <p>I have code that is designed to open a local master file, make additions, and save the file both by overwriting the master file and overwriting a write protected copy on an accessible network location.</p>
  
  <p>But I am unable to replace the existing file on server. I have gone through other link on stackoverflow also like this but still no success.</p>
  
  <p>Kndly assist me ! Rgds The code is </p>
  
  <pre><code>public class UploadAndSaveExcelAction extends Action
  
  {
  
  public ActionForward execute(
  
  ActionMapping mapping,
  
  ActionForm form,
  
  HttpServletRequest request,
  
  HttpServletResponse response) throws Exception{
  
  UploadAndSaveExcelForm myForm = (UploadAndSaveExcelForm)form;
  String target = null;
  if (myForm.getTheExcel().getFileName().length() &gt; 0) {
  FormFile myFile = myForm.getTheExcel();
    System.out.println("" +myFile);
  
  String fileName  = myFile.getFileName();
  
  
  byte[] fileData  = myFile.getFileData();
  
  //Get the servers upload directory real path name
  
   String filePath = getServlet().getServletContext().getRealPath("/")   +"Sheet\SparesUsed.xls";
  
    /* Save file on the server */
   //create the upload folder if not exists
   File folder = new File(filePath);
   if(folder.exists()){
    System.out.println("Excel Sheet folder is  existed therefore deleted");
     folder.deleteOnExit();
  
  
   }
   String filePath1 = getServlet().getServletContext().getRealPath("/") +"Sheet";
   File folder1 = new File(filePath1+"\\" + FileName);
    System.out.println("Excel Sheet afterr delete folder is  "+folder1);
  boolean makedirectory=folder1.mkdir();
  System.out.println(" Making Directory "+makedirectory);
  
   if(!fileName.equals("")){  
  
    System.out.println("Server path for Excel :" +filePath);
  
   //Create file
  
   File fileToCreate = new File(filePath, fileName);
  
   //If file does not exists create file  
  
   if(!fileToCreate.exists()){
  
   FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
  
   fileOutStream.write(fileData);
  
   fileOutStream.flush();
  
   fileOutStream.close();
  target ="success";
   } return mapping.findForward(target);}
  </code></pre>