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"
上一篇: 幽默逗语,认识的挺深刻哦
下一篇: 浅谈Python中copy()方法的使用