关于Streamreaders和Streamwriters属性的选项(Options regarding the properties of Streamreaders and Streamwrite

编程入门 行业动态 更新时间:2024-10-17 06:35:30
关于Streamreaders和Streamwriters属性的选项(Options regarding the properties of Streamreaders and Streamwriters)

在代码中使用streamreader和streamwriters进行登录系统时(可以创建新帐户并可以登录现有帐户)我一直在使用:

usernameReader = New StreamReader("J:\Computing Coursework\real project\KES\Resources\username.txt")

流读取器的“新”部分是必要的(因为变量不能仅仅等于流),但是我想知道是否还有其他选项可以使用而不是'as new streamreader'来打开流而不删除用户名文本文件的内容。

任何帮助,将不胜感激。

When using streamreader and streamwriters in code for a login system (where new accounts can be created and existing accounts can be logged into) I have been using:

usernameReader = New StreamReader("J:\Computing Coursework\real project\KES\Resources\username.txt")

The 'New' part of the streamreader is necessary (as the variable cannot equal just the stream), however I was wondering if there were any other options that I could use instead of the 'as new streamreader' to open the stream without erasing the content of the username text file.

Any help would be appreciated.

最满意答案

如果您不需要为更具体的方案配置StreamReader,也可以使用File.OpenText 。 File类为常见的I / O任务提供了几个有用的快捷方式。

顺便说一句:我不认为打开StreamReader会删除文件; 我宁愿怀疑写入文件的代码是错误的。 如果您打开文件进行写入,它将删除该文件:

writer = new StreamWriter("... path ...")

看看File.AppendText或类似的东西。

You can also use File.OpenText if you do not need to configure the StreamReader for a more specific scenario. The File class provides several useful shortcuts for common I/O tasks.

By the way: I don't think that opening a StreamReader erases the file; I'd rather suspect that the code that writes to the files is wrong. It will erase the file if you open the file for writing like this:

writer = new StreamWriter("... path ...")

Have a look at File.AppendText or something similar.

更多推荐

本文发布于:2023-08-04 02:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1407726.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项   属性   Streamwriters   Streamreaders   Options

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!