Quantcast
Channel: WebSphere Blog by Steve Robinson » WebSphere Jython Examples
Viewing all articles
Browse latest Browse all 10

WebSphere Jython Examples – File Handler Class

$
0
0

Very Basic example of a Jython File-Handler class


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()


Viewing all articles
Browse latest Browse all 10

Trending Articles