JHHK

欢迎来到我的个人网站
行者常至 为者常成

File介绍

目录

概览

Ruby中,File 类是用于处理文件操作的核心类之一。
它提供了许多方法,使得文件的读取、写入、以及其他一些文件操作变得简单而灵活。

1-1 文件是否存才
    File.exist? path


1-2 创建文件
    file = File.new fileName
    file = File.open fileName "w"

2 文件需要打开(r w a)和关闭
    file = File.open path, "r"
    file.close

3 文件内容的读取 r 模式
    str = file.read
    file.each_line do |line| end
    arr = File.readLines

4 文件内容的覆盖和追加(w  a 模式)
    file.puts "hello ruby"

5 转为绝对路径,支持路径拼接
    File.expand_path path1, path2

详细

class XYFile
    # 打开和关闭文件
    def usage1
        puts "----------------- 打开和关闭文件 -----------------"
        # 参数 "r" 表示只读模式,还可以使用 "w"(写入模式)、"a"(追加模式)等。如果文件不存在,写入模式和追加模式会创建新文件。
        file = File.open("./mytest/file.txt", "r")
        puts "file = #{file}"

        # 关闭文件
        file.close
    end


    # 读取文件内容
    def usage2
        puts "----------------- 读取文件内容 -----------------"
        file = File.open("./mytest/file.txt", "r")

        # 读取整个文件内容:
        # content = file.read
        # puts "content = #{content}, content.class = #{content.class}"

        # (上边代码执行了,这次就读取不到内容了):
        # content2 = file.read
        # puts "content2 = #{content2}, content2.class = #{content2.class}"
        # puts "-------------------"

        # 逐行读取文件
        # file.each_line do |line|
        #     puts "line = #{line}"
        # end
        # puts "-------------------"

        # 逐行读取文件:
        lines = file.readlines
        puts "lines = #{lines},lines.class = #{lines.class}"
        lines.each do |line|
            puts "line2 = #{line}"
        end
        puts "第二行内容是:#{lines[1]}"
    end


    #  写入文件内容
    def usage3
        puts "----------------- 写入文件内容 -----------------"

        # 上述代码会创建一个新文件(如果文件不存在),并写入指定的内容。如果文件已存在,原有内容将被覆盖。
        File.open "./mytest/file.txt", "w" do |file|
            # 不是读取模式,会报错
            # content = file.read
            # puts "content = #{content}"

            file.puts "hello ruby"
        end

        # 这会在文件末尾追加新的内容,而不会覆盖原有内容。
        File.open "./mytest/file.txt", "a" do |file|
            file.puts "hello cocoapods"
        end
    end


    # 文件信息和操作
    def usage4
        puts "----------------- 文件信息和操作 -----------------"

        path = "./mytest/file.txt"
        if File.exist? path
            puts "文件存在"

            # 获取文件信息
            # File.stat 方法返回一个 File::Stat 对象,包含文件的各种信息,如文件大小、最后修改时间等。
            stat = File.stat path
            puts "File size: #{stat.size} bytes"
            puts "Last modified: #{stat.mtime}"
        else
            file = File.new(path, "w")
            file.puts "Hello, this is a new file."
            file.close
        end
    end

    # 在打开文件后最好使用 ensure 块确保文件被关闭,以免发生资源泄漏。
    # 这样做可以确保文件在发生异常时也能正确关闭
    def usage5
        begin
            file = File.open("./mytest/file.txt", "r")
        rescue => e
            puts "An error occurred: #{e.message}"
        ensure
            file.close if file
            puts "ensure:来了"
        end
    end


    # 路径拼接
    def usage6

        # 相对路径转为绝对路径
        path = File.expand_path './mytest/file.txt'
        # path = /Users/lixiaoyi/LXYFile/ResourceInCoding/HelloRuby/mytest/file.txt
        puts "path = #{path}"
        puts "-------------"

        # 拼接后转为绝对路径
        path = "./mytest"
        newPath = File.expand_path("file.txt", path)
        # newPath = /Users/lixiaoyi/LXYFile/ResourceInCoding/HelloRuby/mytest/file.txt
        puts "newPath = #{newPath}"
    end



    def self.show
        puts "XYFile self.show invoke"

        file = XYFile.new
        file.usage6
    end
end

行者常至,为者常成!





R
Valine - A simple comment system based on Leancloud.