Ruby file Processing

Source: Internet
Author: User

#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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.