Julia:IO簡介
阿新 • • 發佈:2018-11-06
fname = "test.txt" # using [do] means the file is closed automatically open(fname,"r") do f for line in eachline(f) print(line, "\n") end end #> this is a simple file containing #> text and numbers: #> 12.6 #> 39 f = open(fname,"r") showall(readlines(f)) # 返回值為字串 #> String["this is a simple file containing", "text and numbers:", "12.6", "39"] close(f) println() f = open(fname,"r") fstring = readstring(f) close(f) println(summary(fstring)) #> String print(fstring) #> this is a simple file containing #> text and numbers: #> 43.3 #> 17 outfile = "outfile.txt" # writing to files is very similar: f = open(outfile, "w") # both print and println can be used as usual but with f as their first arugment println(f, "luk name") print(f, "luk name") print(f, " on the same line") close(f) println() outfile_content = open(readstring, outfile, "r") println(repr(outfile_content)) #> "luk name\nluk name on the same line"