sgeenviron.javaΒΆ

/*
 *  Copyright 2010. The Regents of the University of California.
 *  All Rights Reserved.  
 *  @author  Prakashan Korambath
 *
 *  Purpose:  Make use of scheduler TMPDIR variable for File I/O.  The scratch space
 *  defined in SGE is often has a faster I/O than NFS file system.  So your codes
 *  may run faster if you use TMPDIR to write scratch files which are not needed
 *  at the end of a run.  The scheduler used on Hoffman2 is Oracle Grid Engine.
 *  In place of TMPDIR any other faster SCRATCH space can be substituted.
 *
 *  This software program and documentation are copyrighted by The Regents
 *  of the University of California. The software program and documentation
 *  are supplied "as is", without any accompanying services from The Regents.
 *  The Regents does not warrant that the operation of the program will be
 *  uninterrupted or error-free. The end-user understands that the program
 *  was developed for research purposes and is advised not to rely exclusively
 *  on the program for any reason.

 *  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 *  DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
 *  LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
 *  EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY
 *  OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED
 *  HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO
 *  OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
 *  MODIFICATIONS.
 *  
 */

import java.io.*;

class sgeenviron {
    public static void main(String[] args) {
        String tmpdir = System.getenv("TMPDIR");   // Get SGE environment variable TMPDIR

        if (tmpdir==null) {
             System.out.println("Failed to get environment variable TMPDIR"); // Display the string.
             tmpdir="/work";

        }

        System.out.println("TMPDIR is " + tmpdir); // Display the TMPDIR

        String filepath = null;
        String scratch1 = "/scratchxxx";  //Scratch file name .  Do not forget / character
        filepath = tmpdir+scratch1;
      
        // create a file and write something
        
        System.out.println("Full path is " + filepath); // Display the scratch file path
        try 
        {
            File file = new File(filepath);
            boolean success = file.createNewFile();
        }
        catch (IOException e)    {   
            System.err.format("createFile error: %s%n", e);
        }
        String useFullData = null;
        useFullData = "FirstName:" + "\n" + "LastName:" + "\n"; // add some entries
        try { 
                BufferedWriter out = new BufferedWriter(new FileWriter(filepath)); 
                out.write(useFullData); 
                out.close(); 
        } catch (IOException e) { 
                e.printStackTrace();
        } 

        // read something from the file that is already created

        try{
           FileInputStream fileStream = new FileInputStream(filepath);
           DataInputStream input = new DataInputStream(fileStream);
           BufferedReader bufferLine = new BufferedReader(new InputStreamReader(input));
           String Line;
           while ((Line = bufferLine.readLine()) != null)   {
                 System.out.println (Line);
           } 
           input.close();
        }  catch (FileNotFoundException e) {
           e.printStackTrace();
        }  
        catch (Exception e){
            System.err.println("Reading Error: " + e.getMessage());
        }

        File file = new File(filepath);
        boolean success = file.delete();
        if (!success){
          System.out.println("File Deletion failed.");
          System.exit(0);
        }else{
          System.out.println("File  " + file + " deleted.");
        }
    }
}