title: Java encrypt file data and write it on same file
body: <p>I'm working on a project to Encrypt/Decrypt files. as this is my first time, I'm wondering if I'm doing it right or not. till now, my idea about encrypting is this :</p>
  
  <p>Select a file <strong>-></strong> Read all its bytes and add it to byte array <strong>-></strong> Encrypt the byte array <strong>-></strong> write encrypted bytes to same file.</p>
  
  <p>note that in this project output file is same file as input. So I decided to clear file before writing encrypted bytes to it.</p>
  
  <p>This might be stupid (and thats why I'm asking for help), so here is my way </p>
  
  <pre><code>public class Encryptor {
    File file;
    SecretKeySpec secretKeySpec;
    public void setFile(String filePath) throws Exception {
      this.file = new File(filePath);
      if(!file.isFile()){
        throw new Exception("The file you choosed is not valid");
      }
    }
    public void setKey(String keyword){
      try {
        MessageDigest sha = MessageDigest.getInstance("SHA-256");
        sha.update(keyword.getBytes("UTF-8"));
        byte[] key = sha.digest();
        secretKeySpec = new SecretKeySpec(key, "AES");
      } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
        e.printStackTrace();
      }
    }
    public void encrypt(){
      byte[] bFile = new byte[(int) file.length()];
      try {
        //adding portocol bytes to the file bytes
        //String portcol = "encryptor portocol";
        //byte[] decPortocol = portcol.getBytes();
  
        //convert file into array of bytes
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
        bufferedInputStream.read(bFile);
        bufferedInputStream.close();
  
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //outputStream.write(decPortocol);
        outputStream.write(bFile);
  
        byte[] cryptedFileBytes = outputStream.toByteArray();
        //Cipher and encrypting
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] encryptedBytes = cipher.doFinal(cryptedFileBytes);
  
        //Write Encrypted File
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file,false));
        bufferedOutputStream.write(encryptedBytes);
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
      }catch (Exception e){
        e.printStackTrace();
      }
    }
  
  }
  </code></pre>
  
  <p><strong>main question</strong> </p>
  
  <p>are there other ways to read-encrypt-write on same file together at same time? like reading bytes part by part and at same time encrypting that part and overwrite it with encrypted bytes.</p>
  
  <p><strong>Can You Help me more ?</strong></p>
  
  <p>And also Any information about how to make my encrypted files more safe can also be helpful.</p>
  
  <p>and does my program kill RAM ?!</p>
  
  <p><strong>(NOTE)</strong> I'm writing encrypted data on same file for some reasons. I'm not much familiar with how hard drive works. One of my reasons is to prevent file from being recovered later. is there anything I have to know about that ? does what I'm doing prevent unEncrypted file to be recovered later ?</p>
  
  <hr>
  
  <p><strong>EDIT</strong>
  @erickson has pointed out something important in his answer. I got to know that this way of encrypting a file is not safe. What I was considering to prevent <em>too</em>, was preventing file from being recovered later. I mean there is no point to encrypt a file and keep it in your hard drive if you once had it unEncrypted there ! in my experience, everytime I recovered a file, I reached last edits of it and I could never get history of changes. I thought this must be the same if I was not wrong in first place. How can I help preventing data recovery then ?!</p>