title: How to write a file in java at a location which needs super user privileges?
body: <p>I am trying to write a file using java. I am using ubuntu platform. I am able to write it in home folder but it needs super user privileges to be written in /var/foo folder(which is obvious!). How to write a file with super user privileges so that I can write the file in /var/foo folder? Currently my code snippet and the exception are as follows:</p>
  
  <pre><code>public class WritetoFile
  {
  
      public static void main(String[] args) throws IOException
      {
          File f = new File("/var/foo/abc.txt");
  
          if (!f.exists())
      {
          f.createNewFile();
      }
  
          FileWriter fw = new FileWriter(f.getAbsoluteFile());
      BufferedWriter bw = new BufferedWriter(fw);
      bw.write("Hello world!");
          bw.close();
     }
  }
  </code></pre>
  
  <p>when I execute it I get following exception(obviously!)</p>
  
  <pre><code>Exception in thread "main" java.io.IOException: Permission denied
      at java.io.UnixFileSystem.createFileExclusively(Native Method)
      at java.io.File.createNewFile(File.java:959)
      at WritetoFile.main(WritetoFile.java:19)
  </code></pre>
  
  <p>So is there any other Class or way to write the file in /var/foo folder?</p>