Ruby 文件处理
#r read,
#w write,
#a append,
#r+ 读写方式 从文件的头位置开始读取或写入,
#w+ 读写方式,如果文件已存在清空该文件,不存在就创建一个新的文件,
#a+ 如果文件存在就在最后面附加,如果不存在就创建一个新文件。
FileName="newfile.txt"
file=File.open(FileName,'a') file.puts 'test'
p file.path
file.close
p File.file?(FileName)
#Dir.foreach("C:/") { |dir| puts dir }
puts "追加的文件:#{FileName}"
File.open(FileName,'a+') do |io|
(1..10).each do |i|
io.puts "追加的文件:#{FileName}第 #{i}行数据"#写入文件
end
end
File.open(FileName,'r+') { |io|
io.each { |i|
#puts "行号:#{io.lineno}:#{i}"#读取文件
}
}
#File.rename(old_name, new_name)#重命名文件
#File.delete(file_name) #删除文件
file_size= File.size(FileName)#获取文件的字节大小
puts file_size
file=File.open(FileName)
puts "创建时间#{file.stat.ctime}"#创建时间
puts "最后修改时间#{file.stat.mtime}"
puts "最后访问时间#{file.stat.atime}" puts "当前工作目录:#{Dir.pwd}"
if !File.directory?(Dir.pwd+'/testdir')
Dir.mkdir 'testdir'#创建目录
end Dir.foreach(Dir.pwd) do |dir|
#puts dir#列出当前目录下所有文件和子目录
end Dir.chdir('C:/') #更改当前工作目录
puts "当前工作目录:#{Dir.pwd}" #加载当前目录中所有的子目录和文件,会占用大量的内存,另一种方法是使用find模块
#Dir.glob('**/**').each do |filename|
# puts filename
#end #require "find" #包含find模块
#Find.find(Dir.pwd) { |path| puts path } require "rexml/document"
docxml=REXML::Document.new
element=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 #YMAL库
#FPDF库 生成PDF
#Rubyzip库 读写zip文件
#Rmagick库 图像文件处理
Ruby 文件处理的更多相关文章
- 雷林鹏分享:Ruby 文件的输入与输出
Ruby 文件的输入与输出 Ruby 提供了一整套 I/O 相关的方法,在内核(Kernel)模块中实现.所有的 I/O 方法派生自 IO 类. 类 IO 提供了所有基础的方法,比如 read. wr ...
- ubuntu下ruby文件执行蛋疼的一个问题
ubuntu下面用sublime打开非常简单的一段代码(其实不算代码,因为没有实际语句): #!/usr/bin/ruby 在shell下加入x权限:chmod u+x doit.rb,然后运行 ./ ...
- ruby文件操作
Ruby代码 1.#读文件 2.f = File.open("myfile.txt", "r") 3.f.each_line do|line| 4.puts & ...
- Ruby 文件 FILE
FileUtils.makedirs(LOCAL_DIR) unless File.exists?LOCAL_DIR require 'fileutils' Dir.mkdir(DATA_DIR) u ...
- Ruby之入门(一)
前言 这门语言很少去听过,可能是没怎么用到就不会听到太多关于ruby的消息,工作需要这门语言,需要从0开始学习这门语言,慢慢学习简直...太神奇了...,原谅我见识浅薄.原来很早就已经出世了,园子中也 ...
- [Ruby on Rails系列]2、开发环境准备:Ruby on Rails开发环境配置
前情回顾 上次讲到Vmware虚拟机的安装配置以及Scientific Linux 6.X系统的安装.这回我们的主要任务是在Linux操作系统上完成Ruby on Rails开发环境的配置. 在配置环 ...
- Ruby自学笔记(二)— Ruby的一些基础知识
Ruby安装好之后,我们就可以来实践Ruby语言了. 以下是一些学习到的简单基础知识: 1. 如何执行Ruby文件? 我们编写的Ruby文件是以rb为后缀名的,例如:XXX.rb.当要执行ruby文件 ...
- 开发环境准备:Ruby on Rails开发环境配置
开发环境准备:Ruby on Rails开发环境配置 前情回顾 上次讲到Vmware虚拟机的安装配置以及Scientific Linux 6.X系统的安装.这回我们的主要任务是在Linux操作系统上完 ...
- 安装Ruby、Sass与Compass
sass基于Ruby语言开发而成,因此安装sass前需要安装Ruby.(注:mac下自带Ruby无需在安装Ruby!) window下安装SASS首先需要安装Ruby,先从官网下载Ruby并安装.安装 ...
随机推荐
- leetcode Triangle及其思考
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- linux源代码阅读笔记 fork和execve的区别
1. man exec就可以知到: The exec() family of functions replaces the current process image with a new proce ...
- Create a method synchronized without using synchronized keyword
Actually, lots of ways: No need for synchronization at all if you don't have mutable state. No need ...
- poj 1562 Oil Deposits (广搜,简单)
题目 简单的题目,只是测试案例的输入后面可能有空格,所以要注意一下输入方式. #define _CRT_SECURE_NO_WARNINGS //题目的案例输入n,m后面有些貌似有空格... #inc ...
- HDU 1102 Constructing Roads(最小生成树,基础题)
注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...
- poj 3270 Cow Sorting
思路:仔细读题,看到FARMER是两两交换牛的顺序进行排序的话,应该就往置换上靠拢,而这个题果然是置换的应用(有的解题报告上说是置换群,其实这只是单个置换,不用让它构成群).我们来将这些无序的牛抽象成 ...
- Java 编译错误:缺少返回语句
示例: import java.util.*; import java.io.*; public class tt { public static void main(String[] args) { ...
- Openfire 代码部署报错: Variable references non-existent resource:${workspace_loc:openfire_src}
Variable references non-existent resource:${workspace_loc:openfire_src} -DopenfireHome=“${workspace_ ...
- Solr查询详解
前言:上节是关于Solr的开发准备工作:.NET开发过程中的全文索引使用技巧之Solr(http://www.cnblogs.com/johnwood/p/3447242.html) 这节重点是讲So ...
- Google Hacking技术
(1)google hack database https://www.exploit-db.com/google-hacking-database/ (2)查找包含某关键字的特定类型文件 命令:关键 ...