Ruby语言入门(16)- 内部类 -IO

上一篇 / 下一篇  2013-02-04 16:29:26 / 个人分类:语言

IO类实现了基本的输出输入功能。

IO.new(fd[, mode])
IO.for_fd(fd[, mode]) 
IO.open(fd[, mode]) 
IO.open(fd[, mode]) {|io| ... } 
生成并返回一个全新的IO对象,它对应于已打开的文件描述符fd。

IO.for_fd与IO.new相同。IO.open稍有不同,因为它可以带块(块运行结束时fd会被关闭)。带块的IO.open返回块的结果。其他的返回生成的IO对象。

IO.foreach(path[, rs]) {|line| ... }
对path所指文件的各行内容进行迭代操作(与open一样,若path以"|"开头的话则读取命令输出)。


IO.pipe
执行pipe(2)后返回一个数组,该数组包括2个互联的IO对象。返回数组中的首元素负责读取,第二个元素负责读入。

pipe = IO.pipe    
=> [#<IO:fd 3>, #<IO:fd 4>]
pipe[1].puts "test"
pipe[0].gets      
=> "test\n"

IO.popen(command [, mode])
IO.popen(command [, mode]) {|io| ... }
将command作为子进程来运行,然后建立一条连接到该进程的输入输出的管线(pipeline)。

io = IO.popen("cat", "r+")  
=> #<IO:fd 7>
io.puts "test"
io.gets  
=> "test\n"

若带块时,将以生成的IO对象为参数来运行该块,并返回其结果。在块得到运行以后,生成的管道将会自动关闭。

IO.popen("cat", "r+") {|io|
  io.puts "test"
  io.gets
}
=> "test\n"

当命令名为"-"时,Ruby将运行fork(2)来建立一个连接到子进程的输入输出的管线。

不带块的例子

io = IO.popen("-", "r+")
if io.nil?
  # child
  s = gets
  print "child output: " + s
  exit
end

# parent
io.puts "test"
puts io.gets                   
=> "child output: test\n"
io.close

带块的例子

p IO.popen("-", "r+") {|io|
  if io
    # parent
    io.puts "test"
    io.gets
  else
    # child
    s = gets
    puts "child output: " + s
  end
}
=> "child output: test\n"

IO.read(path,[length,[offset]]) 
在path所指文件中,从offset位置开始读入length字节的内容并将其返回。

IO.read("c:\\test.txt")
=>"test1\ntest2\ntest3\n"

IO.readlines(path[, rs])
读入path所指文件的全部内容后,以行为单位将其转化为数组并返回该数组。

IO.readlines("c:\\test.txt")
=>["test1\n","test2\n","test3\n"]

IO.select(reads[, writes[, excepts[, timeout]]])
执行select(2)。传递给reads/writes/excepts的是等待输入的IO(或其子类)的实例的数组。


IO.sysopen(path[, mode [, perm]]) 
打开path所指文件并返回文件描述符。

newfile = IO.systemopen("c:\\test.txt","r+")
=>3
newff = IO.new(newfile)
=>#<IO:fd 4>

self << object
输出object。可以像下面这样使用一连串的<<:

newfile = IO.systemopen("c:\\test.txt")
=>3
newff = IO.new(newfile, "a+")
=>#<IO:fd 4>
newff <<1<<"test"<<"\n"

binmode
将流变为二进制模式。

new1 = newff.binmode

clone
dup
返回一个全新的IO对象,该对象与receiver使用同一个IO

new2 = newff.clone

close
关闭输入输出端口。

new2.close


close_read
关闭读取用的IO。主要用在管道或读写两用的IO对象上。

new2.close_read

close_write
关闭写入用的IO。

new2.close_write

closed?
若端口已被关闭则返回真。

new.closed?

each([rs]) {|line| ... }
each_line([rs]) {|line| ... }
每次从IO端口读入1行来进行相关处理的迭代器。

each_byte {|ch| ... }
每次从IO端口读入1字节的内容。

eof
eof?
若流(stream)到达文件末端时返回真.

fcntl(cmd[, arg])
将对IO执行系统调用fcntl(2)。

fsync
对写入用的IO执行系统调用fsync(2).

fileno
to_i
返回文件描述符的号码。

newfile.fileno
=>2

flush
刷新IO端口的内部缓冲区。 

newfile.flush

getc
从IO端口读入1个字符,并返回该字符对应的Fixnum。

gets([rs])
读入一行,若成功的话就返回读入的字符串。

ioctl(cmd[, arg])
对IO执行系统调用ioctl,并返回其结果。

isatty
tty?
若输出输入端口已与tty结合时,返回真。

lineno
返回当前的行号。

lineno=number
设定行号。

pid
若是由IO.popen所生成的IO端口时,则返回子进程的进程ID。除此以外返回nil。

pos
tell
返回文件指针的当前位置。

pos = n
将文件指针移动到指定位置。与io.seek(pos, IO::SEEK_SET)相同。

print([arg[, ...]])
依次将参数输出到IO端口。

printf(format[, arg[, ...]])
按照format将参数变为字符串,然后输出到self。

putc(ch)
输出字符ch到self。

puts([obj[, ...]])
输出各个obj到self然后换行。

read([length])
read([length[, buf]])
若给出了length时,就读取length字节的内容,然后返回该字符串。

若将某字符串传给第二参数的话,将使用读入的数据来覆盖该字符串对象,并返回读入的数据。

buf="x"*20
=>"xxxxxxxxxxxxxxxxxxxxx"
io=File.open("c:\\test.txt")
io.read(10,buf)
buf
=>"test"

readchar
与IO#getc一样,读入1个字符后返回与该字符相对应的Fixnum。

readline([rs])
与IO#gets一样,读入1行后返回读入的字符串.使

readlines([rs])
读入所有数据后,以行为单位将其转化为数组,并返回该数组。

readpartial(maxlen[, outbuf])
从IO读入最多maxlen的数据,并返回读入的字符串。

reopen(io)
reopen(name[, mode])
将自身转接到io上.若第一参数为字符串时,将把流转接到name所指的文件上。第二参数的默认值为"r"。若省略第二参数时,将原封不动地继承self的模式。

rewind
将文件指针移动到头部。IO#lineno变为 0 。

seek(offset[, whence])
将文件指针由whence移动到offset。whence的值是下列之一。
  • IO::SEEK_SET: 从文件头部开始(默认)
  • IO::SEEK_CUR: 从当前文件指针开始
  • IO::SEEK_END: 从文件尾部开始
  • 省略whence时,其默认值为IO::SEEK_SET。

stat
生成并返回一个包含文件状态的File::Stat对象。

sync
以真和伪来表示当前的输出同步模式。当输出同步模式为真时,每次调用输出函数时都会刷新缓冲区。

sync=newstate
设定输出同步模式。newstate为真则表示同步模式,伪则表示非同步模式。

sysread(length)
sysread(length[, buf]) 
使用read(2)进行输入,并返回包含输入数据的字符串.

sysseek(offset[, whence]) 
与lseek(2)相同

yswrite(string)
使用write(2)来输出string。若string并非字符串,将使用to_s尝试将其变为字符串。

to_io
返回self。

ungetc(char)
送回(unreading)char

write(str)
对IO端口输出str。若str并非字符串,将使用to_s尝试将其变为字符串。

常数:
SEEK_CUR
SEEK_END
SEEK_SET

newfile.SEEK_CUR
newfile.SEEK_END
newfile.SEEK_SET

TAG:

 

评分:0

我来说两句

日历

« 2024-04-23  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 150822
  • 日志数: 185
  • 文件数: 6
  • 建立时间: 2007-08-06
  • 更新时间: 2015-01-06

RSS订阅

Open Toolbar