搜索引擎 网站模板,网站定制哪家正规,成都百度公司在哪里,重庆seo是什么每次都要查#xff0c;真是蛋疼#xff0c;不如一次性总结一下#xff0c;以后再不记得就来这里找好了。以下代码中需要用到的文件名#xff1a;filename ‘testfile.txt’ 读取其中的全部内容#xff1a;File.read(filename)将一个字符串一次性写入这个文件#xff1a;… 每次都要查真是蛋疼不如一次性总结一下以后再不记得就来这里找好了。以下代码中需要用到的文件名filename ‘testfile.txt’ 读取其中的全部内容File.read(filename)将一个字符串一次性写入这个文件File.write(filename, str)读取文件内容依次处理其中的每一行File.foreach(filename) do |line|#process each line
end 判断文件是否存在File.exists?(filename)删除文件File.delete(filename)文件重命名File.rename(filename, new_name)文件的名字file.path # file是一个File对象文件的绝对路径File.absolute_path(filename)当前所在目录Dir.pwd当前登录用户的home目录Dir.home创建文件夹Dir.mkdir(dir_name) #注意如果父目录不存在的话这里无法创建子目录删除文件夹Dir.rmdir(dir_name)创建多级目录(mkdir -p)FileUtils.mkdir_p(path) #这里需要require fileutils找出当前目录下的所有文件或目录Dir.glob(*)找出当前目录下的所有Ruby文件Dir.glob(*.rb)判断目录是否存在Dir.exists?(dir_name)用文件夹和文件组成一个路径File.join(dir_name, filename)当前文件正在执行的文件相对于当前所在目录pwd目录的相对路径__FILE__当前文件正在执行的文件的目录相对于当前所在目录pwd目录的相对路径File.dirname(__FILE__)当前文件正在执行的文件的绝对路径File.expand_path(__FILE__)当前文件正在执行的文件所在目录的绝对路径File.expand_path(File.dirname(__FILE__))最后附上File.open(filename, mode) 中的mode各种取值以及含义 ModeMeaningrRead-only, starts at beginning of file (default mode).rRead-write, starts at beginning of file.wWrite-only, truncates existing file, to zero length or creates a new file for writing.wRead-write, truncates existing file to zero lengthor creates a new file for reading and writing.aWrite-only, starts at end of file if file exists, otherwise creates a new file for writing.aRead-write, starts at end of file if file exists, otherwise creates a new file for reading and writing.bBinary file mode (may appear with any of the key letters listed above). Suppresses EOL - CRLF conversion on Windows. And sets external encoding to ASCII-8BIT unless explicitly specified.tText file mode (may appear with any of the key letters listed above except b).有任何意见或建议或者发现文中任何问题欢迎留言 更多文章请访问个人博客作者邹小创Githubhttps://github.com/ChrisZou邮件happystriving126.com