Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.


Here are some samples of working with files

in boo and .NET/Mono.

Code Block

import System.IO
import System.Reflection

testfile = "newtestfile.txt"
	if File.Exists(testfile):

	//"using" will dispose of (and close) the file stream for you
	using out = StreamWriter(testfile):
		out.WriteLine("  Some text for this file  ")
		out.WriteLine("# ignore this line")
		out.WriteLine("  Some more text  ")

	using input = StreamReader(testfile): //or you can use File.OpenText
		for line in input:
			line = line.Trim()
			if len(line) > 0 and line[0] != char('#'):
				print line
	//an example using enumerate and no "using"
	fileinput = File.OpenText(testfile)
	for index as int, line as string in enumerate(fileinput):
		print "line ${index}:", line.ToUpper()
except e:
	print "Error", e.ToString()

//An example of constructing file paths

//Assembly.GetExecutingAssembly().Location won't work in booi because you are executing a
//dynamic assembly in memory, you have to compile using booc first
rsppath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "boo.rsp")

print Path.GetFileNameWithoutExtension(rsppath)

See also: