Date Created: Sun 28-Aug-2011



    Jython File Handler Class


    class FileHandler :
    def __init__(self, fileName) :
    self.fileName = fileName
    return
    #endDef

    # a file exists if you can open and close it
    def exists(self):
    try:
    f = open(self.fileName)
    f.close()
    return True
    except:
    return False
    #endTry
    #endDef

    def writeText(self,text) :
    try :
    self.outputfile.write(text)
    except :
    print "ERROR writing text to file name: " + self.fileName
    #endTry
    return
    #endDef

    def open(self) :
    print "opening: " + self.fileName
    try :
    self.outputfile=open(self.fileName,'w')
    except :
    print "ERROR opening file name: " + self.fileName + " for writing!"
    #endTry
    print self.fileName + " was sucessfully opened"
    return
    #endDef

    def close(self) :
    print "closing: " + self.fileName
    try :
    self.outputfile.close()
    except :
    print "ERROR closing file name: " + self.fileName + "!"
    #endTry
    print self.fileName + " was sucessfully closed"
    return
    #endDef
    #endClass


    ==============

    Example of use....


    #Get handle to file
    myFh = fileHandler("c:\temp\output.txt")
    #open file for writing
    myFh.open()
    #Overwrites file as this is what we want
    myFh.writeText("this is some text 1")
    #close file as we have finished with it
    myFh.close()

Middleware Mentor - Steven Charles Robinson

About Me

Steve Robinson has been working in IT for over 15 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware consulting. Steve comes from both an administration and development background.

Before moving to JEE, Steve was an accomplished developer and consultant for both IBM Lotus Notes and Microsoft .NET Technologies.

Follow Steve as @stevencrobinson on twitter.

Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Categories

Oracle WebLogic Categories

JBoss Categories

Other Categories