""" Reading files in Cobra relies on the libraries and therefore is pretty the much the same as C# or VB. See the BCL (Base Class Library) docs for File, StreamReader, StreamWriter, TextReader and TextWriter for more. """ class Test def main nl = CobraCore.newLine fileName = 'test-file.text' # write a whole file File.writeAllText(fileName, 'This is a test file.[nl]To make sure there is a file to work with.[nl]') # read a whole file contents = File.readAllText(fileName) assert 'test' in contents # The `in` operator works for strings: <substring> in <string> ==> bool # read line by line num = 1 using sr = File.openText(fileName) while true line = sr.readLine if line is nil, break print '[num]. len=[line.length] "[line.trim]"' num += 1 # be nice about IO errors fileName = 'bad-file-name.text' try contents = File.readAllText(fileName) catch ioe as IOException print 'I/O Error: [ioe.message]' success assert 'test' in contents |
How To Read And Write Files