ruby文件操作.docx

上传人:b****6 文档编号:4385130 上传时间:2022-12-01 格式:DOCX 页数:27 大小:26.20KB
下载 相关 举报
ruby文件操作.docx_第1页
第1页 / 共27页
ruby文件操作.docx_第2页
第2页 / 共27页
ruby文件操作.docx_第3页
第3页 / 共27页
ruby文件操作.docx_第4页
第4页 / 共27页
ruby文件操作.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

ruby文件操作.docx

《ruby文件操作.docx》由会员分享,可在线阅读,更多相关《ruby文件操作.docx(27页珍藏版)》请在冰豆网上搜索。

ruby文件操作.docx

ruby文件操作

1ruby文件操作

关键字:

fileruby

转!

1检测文件是否存在及其大小

FileTest的exist?

方法可以检测一个文件是否存在:

Ruby代码

1.flag=FileTest:

:

exist?

("LochNessMonster")

2.flag=FileTest:

:

exists?

("UFO")

3.#exists?

isasynonymforexist?

Ruby代码

1.flag=FileTest:

:

exist?

("LochNessMonster")

2.flag=FileTest:

:

exists?

("UFO")

3.#exists?

isasynonymforexist?

flag=FileTest:

:

exist?

("LochNessMonster")

flag=FileTest:

:

exists?

("UFO")

#exists?

isasynonymforexist?

如果我们想要知道文件是否有内容,可以使用File:

:

Stat的zero?

方法:

Ruby代码

1.flag=File.new("somefile").stat.zero?

Ruby代码

1.flag=File.new("somefile").stat.zero?

flag=File.new("somefile").stat.zero?

这个将会返回true,这是因为在ruby中0也是true,nil才是false.

所以我们可以使用size?

方法:

Ruby代码

1.ifFile.new("myfile").stat.size?

2.puts"Thefilehascontents."

3.else

4.puts"Thefileisempty."

5.end

Ruby代码

1.ifFile.new("myfile").stat.size?

2.puts"Thefilehascontents."

3.else

4.puts"Thefileisempty."

5.end

ifFile.new("myfile").stat.size?

puts"Thefilehascontents."

else

puts"Thefileisempty."

end

FileTest模块里面也有zero?

和size?

方法:

Ruby代码

1.flag1=FileTest:

:

zero?

("file1")

2.flag2=FileTest:

:

size?

("file2")

Ruby代码

1.flag1=FileTest:

:

zero?

("file1")

2.flag2=FileTest:

:

size?

("file2")

flag1=FileTest:

:

zero?

("file1")

flag2=FileTest:

:

size?

("file2")

这里还有一个size方法:

Ruby代码

1.size1=File.size("file1")

2.size2=File.stat("file2").size

Ruby代码

1.size1=File.size("file1")

2.size2=File.stat("file2").size

size1=File.size("file1")

size2=File.stat("file2").size

2检测特殊文件属性

这边要注意,File类mix了FIleTest模块,并且FileTest模块和File:

:

Stat模块功能上也有很多重复.

unix/linux有面向字符和面向块的设备。

FileTest的方法blockdev?

和chardev?

可以进行测试:

Ruby代码

1.flag1=FileTest:

:

chardev?

("/dev/hdisk0")#false

2.flag2=FileTest:

:

blockdev?

("/dev/hdisk0")#true

Ruby代码

1.flag1=FileTest:

:

chardev?

("/dev/hdisk0")#false

2.flag2=FileTest:

:

blockdev?

("/dev/hdisk0")#true

flag1=FileTest:

:

chardev?

("/dev/hdisk0")#false

flag2=FileTest:

:

blockdev?

("/dev/hdisk0")#true

有时我们想要知道一个流是否联系到了终端,这时我们可以使用IO类的tty?

方法:

Ruby代码

1.flag1=STDIN.tty?

#true

2.flag2=File.new("diskfile").isatty#false

Ruby代码

1.flag1=STDIN.tty?

#true

2.flag2=File.new("diskfile").isatty#false

flag1=STDIN.tty?

#true

flag2=File.new("diskfile").isatty#false

一个流可以是一个管道,或者一个socket:

Ruby代码

1.flag1=FileTest:

:

pipe?

(myfile)

2.flag2=FileTest:

:

socket?

(myfile)

Ruby代码

1.flag1=FileTest:

:

pipe?

(myfile)

2.flag2=FileTest:

:

socket?

(myfile)

flag1=FileTest:

:

pipe?

(myfile)

flag2=FileTest:

:

socket?

(myfile)

要区分目录和普通文件我们这样使用:

Ruby代码

1.file1=File.new("/tmp")

2.file2=File.new("/tmp/myfile")

3.test1=file1.directory?

#true

4.test2=file1.file?

#false

5.test3=file2.directory?

#false

6.test4=file2.file?

#true

Ruby代码

1.file1=File.new("/tmp")

2.file2=File.new("/tmp/myfile")

3.test1=file1.directory?

#true

4.test2=file1.file?

#false

5.test3=file2.directory?

#false

6.test4=file2.file?

#true

file1=File.new("/tmp")

file2=File.new("/tmp/myfile")

test1=file1.directory?

#true

test2=file1.file?

#false

test3=file2.directory?

#false

test4=file2.file?

#true

File还有一个类方法ftype,他将返回流的类型.他也在File:

:

Stat里面,只不过是实例方法.它的返回值可能是下面的字符

串(file、directory、blockSpecial、characterSpecial、fifo、link或socket).

Ruby代码

1.this_kind=File.ftype("/dev/hdisk0")#"blockSpecial"

2.that_kind=File.new("/tmp").stat.ftype#"directory"

Ruby代码

1.this_kind=File.ftype("/dev/hdisk0")#"blockSpecial"

2.that_kind=File.new("/tmp").stat.ftype#"directory"

this_kind=File.ftype("/dev/hdisk0")#"blockSpecial"

that_kind=File.new("/tmp").stat.ftype#"directory"

要测试一个文件是否为另一个文件的链接,可以使用FileTest的symlink?

方法,要计算链接数量,可以使用nlink方法:

Ruby代码

1.File.symlink("yourfile","myfile")#Makealink

2.is_sym=FileTest:

:

symlink?

("myfile")#true

3.hard_count=File.new("myfile").stat.nlink#0

Ruby代码

1.File.symlink("yourfile","myfile")#Makealink

2.is_sym=FileTest:

:

symlink?

("myfile")#true

3.hard_count=File.new("myfile").stat.nlink#0

File.symlink("yourfile","myfile")#Makealink

is_sym=FileTest:

:

symlink?

("myfile")#true

hard_count=File.new("myfile").stat.nlink#0

3使用管道

ruby中使用IO.popen打开管道:

Ruby代码

1.check=IO.popen("spell","r+")

2.check.puts("'Twasbrillig,andtheslithytoves")

3.check.puts("Didgyreandgimbleinthewabe.")

4.check.close_write

5.list=check.readlines

6.list.collect!

{|x|x.chomp}

7.#listisnow%w[brilliggimblegyreslithytoveswabe]

Ruby代码

1.check=IO.popen("spell","r+")

2.check.puts("'Twasbrillig,andtheslithytoves")

3.check.puts("Didgyreandgimbleinthewabe.")

4.check.close_write

5.list=check.readlines

6.list.collect!

{|x|x.chomp}

7.#listisnow%w[brilliggimblegyreslithytoveswabe]

check=IO.popen("spell","r+")

check.puts("'Twasbrillig,andtheslithytoves")

check.puts("Didgyreandgimbleinthewabe.")

check.close_write

list=check.readlines

list.collect!

{|x|x.chomp}

#listisnow%w[brilliggimblegyreslithytoveswabe]

要注意必须调用close_write,如果没有调用它,读取管道的时候,就不能到达文件的末尾.

下面是一个block的形式:

Ruby代码

1.File.popen("/usr/games/fortune")do|pipe|

2.quote=pipe.gets

3.putsquote

4.#Onacleandisk,youcanseekforever.-ThomasSteel

5.end

Ruby代码

1.File.popen("/usr/games/fortune")do|pipe|

2.quote=pipe.gets

3.putsquote

4.#Onacleandisk,youcanseekforever.-ThomasSteel

5.end

File.popen("/usr/games/fortune")do|pipe|

quote=pipe.gets

putsquote

#Onacleandisk,youcanseekforever.-ThomasSteel

end

如果指定了一个字符串"-",那么一个新的ruby实例将被创建.如果指定了一个block,那么这个block将会作为两个独立

的进程运行。

子进程得到nil,父进程得到一个IO对象:

Ruby代码

1.IO.popen("-")do|mypipe|

2.ifmypipe

3.puts"I'mtheparent:

pid=#{Process.pid}"

4.listen=mypipe.gets

5.putslisten

6.else

7.puts"I'mthechild:

pid=#{Process.pid}"

8.end

9.end

10.

11.#Prints:

12.#I'mtheparent:

pid=10580

13.#I'mthechild:

pid=10582

Ruby代码

1.IO.popen("-")do|mypipe|

2.ifmypipe

3.puts"I'mtheparent:

pid=#{Process.pid}"

4.listen=mypipe.gets

5.putslisten

6.else

7.puts"I'mthechild:

pid=#{Process.pid}"

8.end

9.end

10.

11.#Prints:

12.#I'mtheparent:

pid=10580

13.#I'mthechild:

pid=10582

IO.popen("-")do|mypipe|

ifmypipe

puts"I'mtheparent:

pid=#{Process.pid}"

listen=mypipe.gets

putslisten

else

puts"I'mthechild:

pid=#{Process.pid}"

end

end

#Prints:

#I'mtheparent:

pid=10580

#I'mthechild:

pid=10582

pipe方法也返回互相连接的一对管道:

Ruby代码

1.pipe=IO.pipe

2.reader=pipe[0]

3.writer=pipe[1]

4.

5.str=nil

6.thread1=Thread.new(reader,writer)do|reader,writer|

7.#writer.close_write

8.str=reader.gets

9.reader.close

10.end

11.

12.thread2=Thread.new(reader,writer)do|reader,writer|

13.#reader.close_read

14.writer.puts("WhathathGodwrought?

")

15.writer.close

16.end

17.

18.thread1.join

19.thread2.join

20.

21.putsstr#WhathathGodwrought?

Ruby代码

1.pipe=IO.pipe

2.reader=pipe[0]

3.writer=pipe[1]

4.

5.str=nil

6.thread1=Thread.new(reader,writer)do|reader,writer|

7.#writer.close_write

8.str=reader.gets

9.reader.close

10.end

11.

12.thread2=Thread.new(reader,writer)do|reader,writer|

13.#reader.close_read

14.writer.puts("WhathathGodwrought?

")

15.writer.close

16.end

17.

18.thread1.join

19.thread2.join

20.

21.putsstr#WhathathGodwrought?

pipe=IO.pipe

reader=pipe[0]

writer=pipe[1]

str=nil

thread1=Thread.new(reader,writer)do|reader,writer|

#writer.close_write

str=reader.gets

reader.close

end

thread2=Thread.new(reader,writer)do|reader,writer|

#reader.close_read

writer.puts("WhathathGodwrought?

")

writer.close

end

thread1.join

thread2.join

putsstr#WhathathGodwrought?

4使用非阻塞IO

ruby会在后台执行一些操作,使io不会被阻断,因此大部分情况下可以使用ruby线程来管理IO,当一个线程被Io阻塞之

后,另外的线程能够继续执行.

由于ruby的线程不是一个native的线程,因此ruby的线程都在同一个进程里面.

如果你想关闭一个非阻塞io,你可以这样做:

Ruby代码

1.require'io/nonblock'

2.

3.#...

4.

5.test=mysock.nonblock?

#false

6.

7.mysock.nonblock=true#turnoffblocking

8.#...

9.mysock.nonblock=false#turnonagain

10.

11.mysock.nonblock{some_operation(mysock)}

12.#Performsome_operationwithnonblockingsettotrue

13.

14.mysock.nonblock(false){other_operation(mysock)}

15.#Performother_operationwithnon-blockingsettofalse

Ruby代码

1.require'io/nonblock'

2.

3.#...

4.

5.test=mysock.nonblock?

#false

6.

7.mysock.nonblock=true#turnoffblocking

8.#...

9.mysock.nonblock=false#turnonagain

10.

11.mysock.nonblock{some_operation(mysock)}

12.#Performsome_operationwithnonblockingsettotrue

13.

14.mysock.nonblock(false){other_operation(mysock)}

15.#Performother_operationwithnon-blockingsettofalse

require'io/nonblock'

#...

test=mysock.nonblock?

#false

mysock.nonblock=true#turnoffblocking

#...

mysock.nonblock=false#turnonagain

mysock.nonblock{some_operation(mysock)}

#Performsome_operationwithnonblockingsettotrue

mysock.nonblock(false){other_operation(mysock)}

#Performother_operationwithnon-blockingsettofalse

5使用readpartial

readpartial被设计来用于就像socket这样的流.

readpartial要求提供最大长度的参数,如果指定了buffer,那么这个buffer应指向用于存储数据的一个字符串。

Ruby代码

1.data=sock.readpartial(128)#Readatmost128bytes

Ruby代码

1.data=sock.readpartial(128)#Readatmost128bytes

data=sock.readpartial(128)#Readatmost128bytes

readpartial

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 政史地

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1