Ruby 中的文件处理
它是一种处理文件的方式,例如创建新文件、读取文件中的内容、将内容写入文件、将内容附加到文件、重命名文件和删除文件。
文件处理的常用模式
“r” :文件的只读模式。
“r+” :文件的读写模式。
“w” :文件的只写模式。
“w+” :文件的读写模式。
“a” :只写模式,如果文件存在则追加数据,否则将创建一个新文件。
“a+” :读写模式,如果文件存在则追加数据,否则将创建一个新文件。
Creating a new file
We can create a new File using File.new() method for reading, writing or for both, according to the mode string. we can use fileobject.close() method to close that file.
Writing to the file
With the help of syswrite method, you can write content into a file. File needs to be opened in write mode for this method.The new content will over write the old content in an already existing file.
句法
fileobject = File.new("filename.txt", "mode")
fileobject.syswrite("Text to write into the file")
fileobject.close()
下面是创建一个新文件并写入它的实现。
# File Handling Program
# Creating a file
fileobject = File.new("sample.txt", "w+");
# Writing to the file
fileobject.syswrite("File Handling");
# Closing a file
fileobject.close();
输出:
描述:
创建一个名为 sample.txt 的文本文件,具有读写权限。使用 syswrite 方法将内容“文件处理”写入文件。最后关闭文件。当您打开 sample.txt 文件时,它将包含字符串“文件处理”。
Reading a file
There are three different methods to read a file.
1. fileobject.sysread(20) – Return only the first 20 characters from that file
2. fileobject.read – Return the entire content from a file
3. fileobject.readlines – Return the values as an array of lines
sysread Method
The sysread method is also used to read the content of a file. With the help of this method you can open a file in any mode. It will print till n characters mentioned in sysread method from the file.
句法
fileobject = File.new("filename.txt", "r")
fileobject.sysread(20)
fileobject.close()
下面是从文件中读取内容的实现。
# File Handling Program
# Opening a file
fileobject = File.open("sample.txt", "r");
# Reading the first n characters from a file
puts(fileobject.sysread(21));
# Closing a file
fileobject.close();
# Opening a file
fileobject = File.open("sample.txt", "r");
# Read the values as an array of lines
print(fileobject.readlines);
puts
# Closing a file
fileobject.close();
# Opening a file
fileobject = File.open("sample.txt", "r");
# Read the entire content from a file
print(fileobject.read());
# Closing a file
fileobject.close();
输出:
描述:
示例文本文件包含字符串“Ruby 语言中的文件处理”。该文件以只读权限打开。上面的输出显示了读取文件并打印它的不同方式。 sysread 方法只读取 21 个字符,然后打印前 21 个字符。 readlines 方法将值作为行数组读取。 read 方法将整个内容作为字符串从文件中读取。
Renaming and Deleting a file
Ruby files are renamed using rename method and deleted using delete method.
File Inquiries
To check whether the file exists or not
# Rename the file name
puts File.rename("sample.txt", "newSample.txt")
# Delete the existing file
puts File.delete("sample1.txt")
# Checking the old filename is existing or not
puts File.file?("sample.txt")
# Checking the renamed file is exiting or not
puts File.file?("newSample.txt")
# Checking the file have read permission
puts File.readable?("newSample.txt")
# Checking the file have write permission
puts File.writable?("newSample.txt")
输出:
描述:
rename 方法用于将包含旧名称的文件重命名为新名称,rename 方法 print '0' 文件被重命名,否则 print '1'。delete 方法用于删除现有文件,它将如果文件被删除则打印'1',否则打印'0。文件?方法用于检查文件是否存在。如果文件不存在,则返回 false,否则返回 true。在我们的例子中,sample.txt 文本文件不存在,因为我们将它重命名为 newSample.txt 文件,所以它会返回 false。 newSample 文件已存在,因此它将返回 true。 newSample 文件同时具有读取和写入权限,因此对于最后两条语句,它将返回 true。