Ruby简单读取文件

用于获取键盘输入,并输出

 

while line = gets
    puts line
end

 

 按照行读取文件内容

 

File.open("E:/workspaceNew/RubyStudy/test.txt") do |file|
file.each_line{|line| puts "Got #{line.dump}"}
file.close();
end

 

 按照行读取文件,并按照制定的字符串进行分割.本文中以e字母进行分割

 

File.open("E:/workspaceNew/RubyStudy/test.txt") do |file|
file.each_line("e"){|line| puts "Got #{line.dump}"}
file.close();
end

 

 使用IO.foreach读取文件

 

IO.foreach("E:/workspaceNew/RubyStudy/test.txt"){|line| puts line};

 

 可以将读取的文件保存在一个字符串当中

 

str = IO.read("E:/workspaceNew/RubyStudy/test.txt");
  puts str.length;
  puts str[0,30]

 

 也可以将读取的文件保存在一个数组当中

 

arr = IO.readlines("E:/workspaceNew/RubyStudy/test.txt")
puts arr.length;
puts arr[0]

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值