用于获取键盘输入,并输出
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]