`File`对象访问模式的区别(即w+、r+) [英] Difference between the access modes of the `File` object (ie. w+, r+)

查看:14
本文介绍了`File`对象访问模式的区别(即w+、r+)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Ruby 中使用文件时,r+w+ 模式有什么区别?a+ 模式怎么样?

When using files in Ruby, what is the difference between the r+ and w+ modes? What about the a+ mode?

推荐答案

参见 http://www.tutorialspoint.com/ruby/ruby_input_output.htm

引用:

r
只读模式.文件指针位于文件的开头.这是默认模式.

r
Read-only mode. The file pointer is placed at the beginning of the file. This is the default mode.

r+
读写模式.文件指针将位于文件的开头.

r+
Read-write mode. The file pointer will be at the beginning of the file.

w
只写模式.如果文件存在,则覆盖该文件.如果文件不存在,则创建一个新文件进行写入.

w
Write-only mode. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

w+
读写模式.如果文件存在,则覆盖现有文件.如果文件不存在,则创建一个新文件进行读写.

一个
只写模式.如果文件存在,则文件指针位于文件末尾.也就是说,文件处于追加模式.如果文件不存在,它会创建一个新文件用于写入.

a
Write-only mode. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

a+
读写模式.如果文件存在,则文件指针位于文件末尾.文件以追加模式打开.如果文件不存在,它会创建一个新文件进行读写.

(强调我的.)

r+、w+ 和 a+ 都可以读写.w+ 截断文件.a+ 附加.如果文件不存在,w+ 和 a+ 都会创建文件.)

r+, w+, and a+ all do read-write. w+ truncates the file. a+ appends. w+ and a+ both create the file if it does not exist.)

这篇关于`File`对象访问模式的区别(即w+、r+)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆