当前位置:网站首页>Data read / write: realize data read / write function based on C # script in unity
Data read / write: realize data read / write function based on C # script in unity
2022-06-30 06:05:00 【You still have to work hard!】
FileStream File stream
FileStream Class is exposed as file based Stream, Simultaneous read and write operations are supported , Asynchronous read and write operations are also supported . Namespace :System.IO
Declaration method
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
Parameter description
filePath Provide the path of the file to be read
FileMode Enumeration defines various ways to open a file
Append: Open an existing file , And place the cursor at the end of the file . If the file doesn't exist , Create file ;
Create: Create a new file . If the file already exists , Then delete the old file , Then create a new file ;
CreateNew: Specifies that the operating system should create a new file , If the file already exists , Throw an exception ;
Open: Open an existing file , If the file doesn't exist , Throw an exception ;
OpenOrCreate: Specifies that the operating system should open an existing file , If the file doesn't exist , Create a new file with the specified name to open ;
Truncate: Open an existing file , Once the file is opened , Will be truncated to zero byte size . Then we can write new data to the file , But keep the original creation date of the file . If the file doesn't exist , Throw an exception .
FileAccess Enumeration defines various access rights
Read: Gain read access to the file , Then you can read data from the file ( read-only ).
ReadWrite: Get read 、 Write access to the file , Then you can read from the file 、 Write data ( Can read but write ).
Write: Get write access to the file , Data can then be written to a file ( Just write ).
FileShare Enumeration defines the types of access to the same file
Inheritable: Allow file handles to be inherited by child processes .
None: Decline to share current file . Before closing the file , Any request to open the file ( Requests from this process or another process ) Will fail .
Read: Allow subsequent open file reads . If this flag is not specified , Before closing the file , Any request to open the file for reading will fail . however , Even if this flag is specified , Additional permissions may still be required to access the file .
ReadWrite: Allow subsequent open file read or write . If this flag is not specified , Before closing the file , Any request to open the file for reading or writing will fail . however , Even if this flag is specified , Additional permissions may still be required to access the file .
Write: Allow subsequent open file writes . If this flag is not specified , Before closing the file , Any request to open the file for writing will fail . however , Even if this flag is specified , Additional permissions may still be required to access the file .
Delete: Allow subsequent deletion of files .
StreamReader class : Read file stream
stay C# In language ,StreamReader Class is used to read a string from a stream . It is inherited from TextReader class .
Constructors
StreamReader There are many ways to construct classes , There are four common construction methods :
| Construction method | explain |
|---|---|
| StreamReader(Stream stream) | Creates a for the specified stream StreamReader Class |
| StreamReader(string path) | Create a file for the specified path StreamReader Class |
| StreamReader(Stream stream,Encoding encoding) | Initializes the specified stream with the specified character encoding StreamReader Class |
| StreamReader(String path, Encoding encoding) | Initializes the specified file with the specified character encoding StreamReader Class |
Encoding Coding format commonly used in
- Default: Current operating system ANSI Code page encoding
- Unicode: Use Little-Endian Byte order UTF-16 Encoding of format
- UTF-8:UTF-8 code
Member attribute 、 Member functions
StreamReader The properties and methods commonly used in the class are as follows :
| Properties or methods | effect |
|---|---|
| Encoding CurrentEncoding | Read-only property , Gets the encoding method used in the current stream |
| bool EndOfSteam | Read-only property , Gets whether the current stream position is at the end of the stream |
| void Close() | Closed flow |
| int Peek() | Gets the integer of the next character in the stream , If no characters are obtained , Then return to -1. |
| int Read() | Gets the integer of the next character in the stream |
| int Read(char[] buffer, int index, int count) | Reads the specified maximum number of characters from the current stream into the buffer starting at the specified index position |
| string ReadLine() | Reads a line of characters from the current stream and returns the data as a string |
| string ReadToEnd() | Read all characters from the current position of the stream to the result |
Code instance
Get the file stream first , In constructing through file stream StreamReader To read the file stream , It can also be constructed directly through the path StreamReader To read the file stream ( Refer to the following write file stream method ).
class Program
{
static void Main(string[] args)
{
// File path
string filePath = Application.dataPath + "/Data/task.csv";
// Open and get the file stream according to the file path , And get the encoding format of the file stream
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
// Use the corresponding character encoding format of the file stream to read the file stream
StreamReader streamReader = new StreamReader(fileStream);
// Record one line of records read at a time
string strLine;
// Read line by line CSV Data in
while ((strLine = streamReader.ReadLine()) != null)
{
// Operate on data
Console.WriteLine(strLine);
}
streamReader.Close();
}
}
StreamWriter class : write file
stay C# In language ,StreamWriter Class is used to write a string to a stream .
Constructors
StreamWriter There are many ways to construct classes , There are four common construction methods :
| Construction method | explain |
|---|---|
| StreamWriter(Stream stream) | Creates a for the specified stream StreamWriter Class |
| StreamWriter(string path) | Create a file for the specified path StreamWriter Class |
| StreamWriter(Stream stream,Encoding encoding) | Initializes the specified stream with the specified character encoding StreamWriter Class |
| StreamWriter(String path, Encoding encoding) | Initializes the specified file with the specified character encoding StreamWriter Class |
Member attribute 、 Member functions
StreamWriter The properties and methods commonly used in the class are as follows :
| Properties or methods | effect |
|---|---|
| bool AutoFlush | attribute , Gets or sets whether the buffer is automatically flushed |
| Encoding Encoding | Read-only property , Gets the encoding method used in the current stream |
| void Close() | Closed flow |
| void Flush() | Refresh buffer |
| int Write(char value) | Write characters to the stream |
| void WriteLine(char value) | Writes a character wrap to the stream |
| Task WriteAsync(char value) | Write characters asynchronously to the stream |
| Task WriteLineAsync(char value) | Writes characters asynchronously to the stream |
Code instance
Construct directly through a path StreamWriter To read the file stream , You can also get the file stream first , In constructing through file stream StreamReader To read the file stream ( Refer to the above reading file stream mode ).
public void WriteCsv(string strs, string path)
{
if (!File.Exists(path))
{
File.Create(path).Dispose();
}
else
{
File.Delete(path);
File.Create(path).Dispose();
}
string mystr;
//UTF-8 Way, ,true Indicates the additional write mode , Change to false Is to write directly
using (StreamWriter stream = new StreamWriter(path, true, Encoding.UTF8))
{
for (int i = 0; i < Data.SolList.Count; i++)
{
mystr = "";
Solution sol = Data.SolList[i];
//mystr Save a row of data , Use between elements of different columns "," separate
mystr = sol.SolutionID.ToString() + "," + sol.MakeSpan.ToString() + "," + sol.WaitTime.ToString() + "," + sol.Balance.ToString() + "," + sol.Category;
stream.WriteLine(mystr);
}
}
}
边栏推荐
- 观察者模式、状态模式在实际工作中的使用
- Network basics
- Related applications of priority queue
- requests. The difference between session () sending requests and using requests to send requests directly
- Installation and initialization of MariaDB database
- 583. 两个字符串的删除操作-动态规划
- After getting these performance test decomposition operations, your test path will be more smooth
- Summary of 2 billion redis data migration
- 云服务器部署 Web 项目
- Go common judgments
猜你喜欢

Summary of redis learning notes (I)

SHELL

MySQL日志管理、数据备份、恢复
![[road of system analyst] collection of wrong topics in Project Management Chapter](/img/8b/2908cd282f5e505efe5223b4c5ddbf.jpg)
[road of system analyst] collection of wrong topics in Project Management Chapter
![[deep learning] data segmentation](/img/16/798881bbee66faa2fb8d9396155010.jpg)
[deep learning] data segmentation

inno setup 最简单的自定义界面效果

Configure the user to log in to the device through telnet -- AAA local authentication

SparseArray

从底层结构开始学习FPGA----RAM IP核及关键参数介绍

MySQL storage system
随机推荐
Using lazy < t > in C # to realize singleton mode in WPF
Shenzhou ares tx6 boot logo modification tutorial
Summary of redis learning notes (I)
MySQL事物
Intelligent question - horse racing question
反编译正常回编译出现问题自己解决办法
PC viewing WiFi password
24、 I / O device model (serial port / keyboard / disk / printer / bus / interrupt controller /dma and GPU)
MySQL log management, data backup and recovery
InputStream转InputStreamSource
Common NPM install errors
Codeforces C. Andrew and Stones
[ansible series] fundamentals -01
Several commands not commonly used in MySQL
ES6解构赋值
Attempt to redefine 'timeout' at line 2 solution
重构之美:当多线程批处理任务挑起大梁 - 万能脚手架
There is a group of students' score {99, 85, 82, 63, 60}. To add a student's score, insert it into the score sequence and keep the descending order
IP TCP UDP network encryption suite format
Here comes the nearest chance to Ali