File.basename(‘/home/test/ruby.rb‘) #=> ruby.rb File.basename(‘/home/test/ruby.rb‘, ‘.rb‘) #=> ruby File.basename(‘/home/test/ruby.rb‘, ‘.*‘) #=> ruby
File.dirname(‘/home/test/ruby.rb‘) #=> /home/test File.dirname(‘ruby.rb‘) #=> .
File.extname(‘test.rb‘) #=> .rb File.extname(‘a/b/c/test.rb‘) #=> .rb File.extname(‘text‘) #=> ""
File.expand_path(‘test‘) #=> D:/Program Files/RubyMine 2017.1.5/workspace/File/test File.expand_path(‘test‘, ‘D:/‘) #=> D:/test
File.split(‘/home/test/ruby.rb‘) #=> [‘/home/test‘, ‘ruby.rb‘] File.split(‘ruby.rb‘) #=> [‘.‘, ‘ruby.rb‘]
File.join(‘user‘, ‘mail‘, ‘ruby.rb‘) #=> user/mail/ruby.rb
File.rename(‘1.txt‘, ‘2.txt‘)
File.directory?(‘.‘) #-> true
File.exist?(‘test.txt‘) #-> true
File.file?(‘test.txt‘) #=> true File.file?(‘.‘) #=> false
File.socket?(‘test.txt‘) #=> false
File.new(filename, modestring=‘r‘) -> file
File.new(filename <, modenum <, permission>>) -> file
File.new(fd <, modenum <, permission>>) -> file
根据模式modestring(默认为r)打开名为filename的文件(或关联已经打开的文件描述符fd)并返回一个新的File对象
File.new(‘afile‘, ‘r‘) File.new(‘bfile‘, ‘w+‘) File.new(‘cfile‘, File::CREAT|File::TRUNC|File::RDWR)
File.open(filename, modestring=‘r‘) -> file
File.open(filename <, modenum <, permission>>) -> file
File.open(fd <, modenum <, permission>>) -> file
File.open(filename, modestring=‘r‘){|file| block } -> obj
File.open(filename <, modenum <, permission>>){|file| block } -> obj
File.open(fd <, modenum <, permission>>){|file| block } -> obj
如果没有关联的block,那么open和File.new同义,如果提供了代码block,他将传递file作为参数,
并且当block执行结束时文件将被自动关闭,这种情况下,File.open返回block的值
File.readable?(‘test.txt‘) #=> true
File.writable?(‘test.txt‘) #-> true
File.size(‘test.txt‘) #=> 9
原文:https://www.cnblogs.com/wf0117/p/9043495.html