欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Julia:IO简介

程序员文章站 2022-04-28 09:36:19
...
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"

 

相关标签: julia