繁体   English   中英

.size是什么意思

[英]What Does .size mean

以下代码中的target.size是什么意思,文件名旁边的w是什么意思? 当我删除w.size程序时,这是什么意思?

filename = ARGV.first
    script = $0

    puts "We're going to erase #{filename}."
    puts "If you don't wnat that, hit CTRL-C (^C)."
    puts "If you do want that, hit RETURN."

    print "? "
    STDIN.gets

    puts "Opening the file..."
    target = File.open(filename,'w')

    puts "Truncating the file. Goodbye!"
    target.truncate(target.size)

    puts "Now I'm going to ask you for three lines."

    print "line 1: "; line1 = STDIN.gets.chomp()
    print "line 2: "; line2 = STDIN.gets.chomp()
    print "line 3: "; line3 = STDIN.gets.chomp()

    puts "I'm going to write these to the file"

    target.write(line1)
    target.write("\n")
    target.write(line2)
    target.write("\n")
    target.write(line3)
    target.write("\n")
    puts "And finally, we Close it"
    target.close()

File.open(filename,'w')给您一个File对象,该对象分配给局部变量target target.size实际上是File#size ,它返回文件的大小(以字节为单位)。 如果未将模式提供为'w' ,则文件将以'r'模式打开,这是默认模式。


读取IO打开模式

"w"

只写,将现​​有文件截断为零长度或创建一个新文件进行写入。


关于target.truncate(target.size)行的说明。

在这里,您实际上称为File#truncate ,此方法的作用是将文件截断为最大整数字节 这意味着您将完全删除文件内容并使文件大小为零

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM