成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

深度解析Ruby文件操作

開發 開發工具
Ruby文件操作對于一個經驗豐富的編程人員來說是比較容易掌握的技巧。通過這篇文章,我們可以學到關于Ruby文件操作的一些技巧。

Ruby文件操作是一個比較常用的一個編程方法。在接下來的這篇文章中我們將會為大家詳細介紹有關Ruby文件操作的一些實現技巧。#t#

Ruby文件操作代碼示例如下:

#2.rb;在同一級目錄建立1.txt的文件,輸入一些內容

file=File.open("1.txt","r")#file=File.open("F:\\ruby\\rb\\1.txt","r")絕對路徑下
file.each_line do |line|
puts line
end

# another logic

File.open("1.txt","r") do |file|
while line=file.gets
puts line
end
end

# the third logic ,the code is copied from someone else ...

IO.foreach("1.txt") do |line|
#puts line if line =~/target/
puts line if line !~/target/
end

#count the number of a file ,the bytes and lines

arr=IO.read("1.txt")
bytes=arr.size
puts "the byte size is #{bytes}"
arrl=IO.readlines("1.txt")
lines = arrl.size
puts"the lines number is #{lines}"

#show the file's path

puts File.expand_path("1.txt")

#count chars from a file


file= File.new("1.txt")
w_count = 0
file.each_byte do |byte|
w_count += 1 if byte ==?1

end
puts "#{w_count}"


#create new file and write some words there

file= File.new("2.txt","w")

puts File.exist?("2.txt")#judge the file is exist or not
file.write("hehe\nhahah")

#io.stream operation

require 'stringio'

ios = StringIO.new("abcdef\n ABC \n 12345")
ios.seek(5)
ios.puts("xyz3")
puts ios.tell

puts ios.string.dump

#the result is 10,insert xyz3 at the 5th byte


#another example

require 'stringio'

ios = StringIO.new("abcdef\n ABC \n 12345")
ios.seek(3)
ios.ungetc(?w) #replace the char at index 3

puts "Ptr = #{ios.tell}"
s1 = ios.gets #filte the "\n"
s2 = ios.gets
puts s1
puts s2

希望上面這段代碼示例可以幫助我們熟練掌握Ruby文件操作的一些技巧。

責任編輯:曹凱 來源: 百度博客
相關推薦

2009-12-16 11:04:51

Ruby操作文件權限

2010-04-14 15:32:18

Unix操作系統

2009-12-16 10:49:42

Ruby操作二進制文件

2009-12-18 15:56:05

Ruby特殊語法

2009-12-18 16:27:41

Ruby解析Json

2009-12-15 15:55:43

Ruby模塊

2009-09-01 10:28:38

C#追加文件

2009-12-17 17:37:42

Ruby on Rai

2009-12-15 13:59:42

Ruby對象操作

2024-01-11 12:14:31

Async線程池任務

2009-09-01 10:10:51

C# StreamRe

2009-12-17 13:57:15

Ruby關鍵字

2009-06-22 13:40:00

RubyJava

2025-01-13 00:30:17

2025-05-12 01:33:00

異步函數Promise

2009-12-21 11:19:50

WCF配置文件

2010-04-22 13:56:21

Aix操作系統

2009-12-18 16:00:29

Ruby獲取當前類名

2013-12-09 10:34:12

2023-03-06 11:13:20

Spring注解加載
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品视频网 | 日韩中文字幕久久 | 美女视频一区二区三区 | 成人av看片| 精品永久| 日韩二三区 | 亚洲一二三区在线观看 | 久久久久亚洲视频 | 欧美黑人巨大videos精品 | 婷婷激情综合 | aaaa日韩 | 中文字幕视频网 | 欧美日韩精品一区二区三区四区 | 欧美日韩高清在线观看 | 国产亚洲欧美另类一区二区三区 | 日韩精品 电影一区 亚洲 | 精品国产91亚洲一区二区三区www | 国产精品久久久久久久久久免费看 | 日本电影免费完整观看 | 一级做a毛片 | 日韩欧美在线观看视频网站 | 91婷婷韩国欧美一区二区 | 欧美 日韩 视频 | 日韩三级在线 | 看av电影 | 亚洲一区在线播放 | 免费亚洲婷婷 | 欧美中文在线 | 欧美日韩91 | 97久久精品午夜一区二区 | 成人午夜免费福利视频 | 亚洲国产高清高潮精品美女 | 亚洲 欧美 日韩 精品 | 亚洲精品中文字幕 | 亚洲国产成人精品一区二区 | 国产激情在线播放 | 精品国产一区二区三区久久久蜜月 | 欧美日韩国产精品一区 | 久久精品一区二区三区四区 | 激情六月丁香 | 亚洲大片|