#R Read,#W Write,#a Append,#r+ Read or write from the head position of the file.#w+ Read-write mode, if the file already exists to empty the file, does not exist to create a new file,#A + If the file exists on the last side attached, create a new file if it does not exist. Filename="Newfile.txt"file=file.open (FileName,'a') file.puts'Test'p file.pathfile.closep file.file? (FileName)#Dir.foreach ("c:/") {|dir| Puts dir}Puts"appended files: #{filename}"File.Open (FileName,'A +') Do |io| (1..10). Each do |i|io.puts"appended file: #{filename} #{i} row data"#Write FileEndendfile.open (FileName,'r+') {|io|Io.each {|i|#puts "line number: #{io.lineno}:#{i}" #读取文件 } }#file.rename (Old_name, new_name) #重命名文件#File.delete (file_name) #删除文件File_size= file.size (FileName)#gets the byte size of the fileputs File_sizefile=File.Open (FileName) puts"creation Time #{file.stat.ctime}"#creation TimePuts"last modified time #{file.stat.mtime}"puts"last access time #{file.stat.atime}"puts"Current working directory: #{dir.pwd}"if! File.directory? (dir.pwd+'/testdir') Dir.mkdir'TestDir'#Create a directoryEnddir.foreach (dir.pwd) do|dir|#puts dir# lists all files and subdirectories in the current directoryEnddir.chdir ('c:/')#change the current working directoryPuts"Current working directory: #{dir.pwd}"#loading all subdirectories and files in the current directory can consume a lot of memory, and the other way is to use the Find module#dir.glob (' **/** '). each do |filename|#puts filename#End#require "find" #包含find模块#Find.find (dir.pwd) {|path| puts path}require"rexml/document"Docxml=rexml::D ocument.newelement=docxml.add_element (' Book',{'name'='Ruby Book'}) Chapter1=element.add_element ('C1',{'title'='C11'}) Chapter2=element.add_element ('C2',{'title'='C22'}) Chapter1.add_text'Chapter1'Chapter2.add_text'Chapter2'Docxml.write
Ruby file Processing