1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#建立一个222.rb文件并且输入字符
file = File .open( "222.rb" , "w+" )
file.puts "123\nwadwa\n12124124\ndwdw"
file.close
#输出222.rb的内容
File .open( "222.rb" , "r+" ) do |file|
while line = file.gets
puts line
end
end
#直接用IO操作文件
IO .foreach( "222.rb" ) do |line|
puts line if line =~/abc/ #输出匹配到了'abc'的所在行
puts line if line !~/qwe/ #输出没有匹配到'qwe'的所在行
end
#输出文件的绝对路径
puts File .expand_path( "222.rb" )
#count chars from a file
file= File . new ( "222.rb" )
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
print "The file now is exist? --> "
puts File .exist?( "asd.txt" ) #判断文件是否存在
file= File . new ( "asd.txt" , "w" )
print "The file now is exist? --> "
puts File .exist?( "asd.txt" )
file.write( "hehe\nhahah" )
#io.stream operation
require 'stringio'
ios = StringIO. new ( "abcdef\n ABC \n 12345" )
ios.seek( 5 ) #把偏移指针移到5(e字母所在位置)
ios.puts( "xyz3" ) #从5开始覆写原有数据
puts ios.tell #tell--Returns the current offset (in bytes) of ios.
puts ios.string
puts ios.string.dump #忽略\n的转义
#another example
require 'stringio'
ios = StringIO. new ( "abcdef\nq9ert \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打开文件并写入数据操作
txt = File .open( "文件路径" , "w+" )
txt.puts '要写入的文件内容'
txt.close
#从文件里读取数据
num = File .readlines( "文件路径" )[ 0 ].chomp
#打开文件的方法
system( "notepad 文件路径" )
|
相关文章
- 归纳整理Linux下C语言常用的库函数----文件操作
- hadoop HDFS常用文件操作命令
- Linux下vim基本操作和清空文件内容的常用方法
- 通过JAVA代码对yaml文件进行增加、修改的操作
- Log4j2介绍和特性实例(七)--代码中指定日志文件的名字
- EduCoder-Java面向对象(第四章)-String类 的实现代码 关卡三 String串类操作练习 - 文件名与邮箱验证
- C#委托(delegate)的常用方式- 委托的定义 // 委托的核心是跟委托的函数结构一样 public delegate string SayHello(string c); public delegate string SayHello(string c);:定义了一个公共委托类型 SayHello,该委托接受一个 string 类型的参数 c,并返回一个 string 类型的值。 Main 方法 static void Main(string args) { // 本质上其实就是把方法当作委托的参数 SayHello sayC = new SayHello(SayChinese); Console.WriteLine(sayC("欢迎大家")); SayHello sayE = new SayHello(SayEgnlish); Console.WriteLine(sayE("Welcome to")); // 简单的写法:必须类型一样 SayHello s1 = SayChinese; SayHello s2 = SayEgnlish; Console.WriteLine(s1("好好好")); Console.WriteLine(s2("Gooood")); // 最推荐 SayHello ss1 = con => con; Console.WriteLine(ss1("niiiice")); // 匿名委托:一次性委托 SayHello ss3 = delegate(string s) { return s; }; Console.WriteLine(ss3("说中国话")); } 常规实例化委托 SayHello sayC = new SayHello(SayChinese);:创建了一个 SayHello 委托的实例 sayC,并将 SayChinese 方法作为参数传递给委托的构造函数。 Console.WriteLine(sayC("欢迎大家"));:通过委托实例调用 SayChinese 方法,并输出结果。 同理,SayHello sayE = new SayHello(SayEgnlish); 和 Console.WriteLine(sayE("Welcome to")); 是对 SayEgnlish 方法的委托调用。 简化的委托赋值方式 SayHello s1 = SayChinese; 和 SayHello s2 = SayEgnlish;:当委托类型和方法签名一致时,可以直接将方法赋值给委托变量,无需使用 new 关键字。 Console.WriteLine(s1("好好好")); 和 Console.WriteLine(s2("Gooood"));:通过委托实例调用相应的方法。 使用 Lambda 表达式实例化委托 SayHello ss1 = con => con;:使用 Lambda 表达式创建委托实例 ss1,con => con 表示接受一个参数 con 并返回该参数本身。 Console.WriteLine(ss1("niiiice"));:通过委托实例调用 Lambda 表达式。 匿名委托 SayHello ss3 = delegate(string s) { return s; };:使用匿名委托创建委托实例 ss3,delegate(string s) { return s; } 是一个匿名方法,直接在委托实例化时定义了方法体。 Console.WriteLine(ss3("说中国话"));:通过委托实例调用匿名方法。 委托引用的方法定义 public static string SayChinese(string content) { return content; } public static string SayEgnlish(string content) { return content; } public static string SayChinese(string content) 和 public static string SayEgnlish(string content):定义了两个静态方法,分别接受一个 string 类型的参数 content,并返回该参数本身。这两个方法的签名与 SayHello 委托一致,可以被 SayHello 委托引用。 常规的委托实例化、简化的赋值方式、Lambda 表达式和匿名委托。委托在 C# 中是一种强大的机制,它允许将方法作为参数传递,实现了代码的灵活性和可扩展性。
- filesystem库——C++文件操作(基本语法,常用函数封装)
- ASP.NET网页中RAR、DOC、PDF等文件下载功能实例源代码
- linux常用命令之--目录与文件的操作命令