Writing text files .net
When the ReadLine method reaches the end of the file, it returns a null reference. For more information, see StreamReader Class. On the File menu, point to New , and then select Project. On the Debug menu, select Start to compile and to run the application. The Console window displays the contents of the Sample. The following code uses the StreamWriter class to open, to write, and to close the text file.
In a similar way to the StreamReader class, you can pass the path of a text file to the StreamWriter constructor to open the file automatically. The WriteLine method writes a complete line of text to the text file.
This code creates a file that is named Test. Reading from a text file The System. StreamReader class supports several methods for reading text files and offers a way of determining whether you are at the end of the file that's different from previous versions of Visual Basic.
Line-by-line Reading a text file line-by-line is straightforward. We can read each line with a ReadLine method. To determine whether we have reached the end of the file, we call the Peek method of the StreamReader object.
The Peek method reads the next character in the file without changing the place that we are currently reading. If we have reached the end of the file, Peek returns Listing B provides an example for reading a file line-by-line until the end of the file. Since reading an entire file can mean reading a large amount of data, be sure that the string can handle that much data.
One character at a time If you need to read the file a character at a time, you can use the Read method. This method returns the integer character value of each character read. Listing C demonstrates how to use the Read method. Tap into the power We've barely scratched the surface of the new file functionality included in. NET, but at least you have an idea of the power now available in the latest edition of Visual Basic.
The abilities of the classes in the System. IO namespace are quite useful, but if you want to continue to use the traditional Visual Basic file operations, those are still supported. NET What types of. If you would like to see code comments translated to languages other than English, let us know in this GitHub discussion issue. The following example shows how to use the StreamWriter class to synchronously append text to the text file created in the first example. The following example shows how to asynchronously write text to a new file using the StreamWriter class.
To invoke the WriteAsync method, the method call must be within an async method. The C example requires C 7. The following example shows how to write text to a new file and append new lines of text to the same file using the File class. If the path you provide to the WriteAllText method already exists, the file is overwritten.
The first two examples use static convenience methods on the System. The third example shows how to add text to a file when you have to process each line individually as you write to the file. In the first three examples, you overwrite all existing content in the file.
The final example shows how to append text to an existing file. These examples all write string literals to files. If you want to format text written to a file, use the Format method or C string interpolation feature.
0コメント