title: How to overwrite a file in sdcard in android?
body: <p>I need to read a file in the sd card in my android device and write the contents of this file into another file in the sd card which is already existing.</p>
  
  <p>Here is my code to read a file anywhere in the sdcard.</p>
  
  <pre><code>public String readFromFile(String fileName) {
  
      String ret = "";
      try {
  
          File sdcard = Environment.getExternalStorageDirectory();
          //Get the text file
          File file = new File(sdcard, fileName);
  
  
          BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
  
          if ( bufferedReader != null ) {
  
  
              String receiveString = "";
              StringBuilder stringBuilder = new StringBuilder();
  
              while ( (receiveString = bufferedReader.readLine()) != null ) {
                  stringBuilder.append(receiveString);
              }
  
              ret = stringBuilder.toString();
          }
      }
      catch (FileNotFoundException e) {
          Log.e("login activity", "File not found: " + e.toString());
      } catch (IOException e) {
          Log.e("login activity", "Can not read file: " + e.toString());
      }
  
      return ret;
  }
  </code></pre>
  
  <p>Can some one please tell me how can I copy the contents of this file into another file on sdcard after reading.</p>
  
  <p>I do not wish to append but to overwrite the contents of the file.</p>
  
  <p>I need a method in this format</p>
  
  <pre><code>void writeFile(String fileName, String Data){
  
  //code to overwite with given data
  
  }
  </code></pre>
  
  <p>Can someone please help me</p>
  
  <p>Thanks in advance.</p>