A File object represents a File. It provides some useful methods to check whether the File exists, rename the File, and delete the File. All these methods are static. That is to say, you cannot use the NEW operator to create a File instance, but you can directly use the File method.
If (File. Existas ("File.txt "))
File. Delete ("File.txt ");
You can also use a File object to obtain the File stream used to read and write File data ).
FileStream fs = File. OpenRead ("File. any ");
FileStream fs = File. OpenText ("File.txt ");
The following are some useful File methods:
------------------------------------------------
File. FileExists (fileName)
File. Delete (fileName)
File. AppendText (String)
File. Copy (fromFile, toFile)
File. Move (fromFile, toFile)
File. GetExtension (fileName) // obtain the File extension.
File. HasExtension (fileName) // returns true if the File has an extension.
-----------------------------------------------------------
To read a text File, first use the File object to obtain a StreamReader object, and then use the text stream to read data.
StreamReader ts = File. OpenText ("File.txt ");
String s = ts. ReadLine ();
To create a text file and write data, you can use the CreateText method to obtain-StreamWrite objects.
// Open for writing
StreamWriter sw = File. CreateText ("File.txt ");
Sw. WriteLine ("Hello file ");
If you want to append data to an existing file, you can directly create a StreamWrite object and set the Boolean parameter used for append to true.
// Append to text file
StreamWriter sw = new StreamWriter ("file.txt", true );
========================================================== ==================
A large number of common exceptions occur when processing the input and output of files. Invalid file name, file does not exist, directory does not exist, invalid file name parameters and file protection errors will cause exceptions. Therefore, the best way to process file input and output is
Put the File Processing code in the try block to ensure that all possible errors are captured and fatal errors are avoided.
The methods of various file classes generate the exceptions they can throw in their documents. You only need to capture the common Ex with a required object to ensure
Capture all exceptions, but if you need to handle different exceptions, you must detect the exceptions separately.
For example, you can open a text file in the following ways.
Try
{
StreamReader ts = File. OpenText ("File.txt ");
String s = ts. ReadLine ();
}
Catch (Exception e)
{
Console. writeLine (e. Message );
}
Detection file end
There are two available methods to ensure that the end of the text file is not exceeded: (1) null query exception, (2) Data Query
The end of the stream. When the data read exceeds the end of a text file, no error occurs or an exception occurs. However, if a null value is returned for a single character after the end of the file, you can use it to create a file end function in a file read human.
Private StreamReader rf;
Private bool eof;
//-----------------------------------
Public String readLine ()
{
String s = rf. ReadLine ();
If (s = null)
Eof = true;
Return s;
}
//-----------------------------------
Public bool fEof ()
{
Return eof;
}
Another way to ensure that the read data does not exceed the end of the file is to use Stream's Peek method to view the data before reading it. The Peek method returns the ASCII code of the next character in the file. If there are no characters,-1 is returned.
Public string read_line ()
{
String s = "";
If (rf. Peek ()> 0)
S = rf. ReadLine ();
Else
Eof = true;
Return s;
}
==========================================
CsFile. cs class
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;
Namespace OperateFile
{
Public class csFile
{
Private string fileName;
StreamReader ts;
StreamWriter ws;
Private bool opened, writeOpened;
Public csFile ()
{
Init ();
}
Public csFile (string file_name)
{
FileName = file_name;
Init ();
}
Private void init ()
{
Opened = false;
WriteOpened = false;
}
// Open the file for reading data
Public bool OpenForRead ()
{
Return OpenForRead (fileName );
}
// Open the file for reading data
Public bool OpenForRead (string file_name)
{
FileName = file_name;
Try
{
Ts = new StreamReader (fileName );
Opened = true;
}
Catch (FileNotFoundException e)
{
Return false;
}
Return true;
}
// Read data from a file
Public string readLine ()
{
Return ts. ReadLine ();
}
// Write an object
Public void writeLine (string s)
{
Ws. WriteLine (s );
}
Public bool OpenForWrite ()
{
Return OpenForWrite (fileName );
}
Public bool OpenForWrite (string file_name)
{
Try
{
Ws = new StreamWriter (file_name );
FileName = file_name;
WriteOpened = true;
Return true;
}
Catch (FileNotFoundException e)
{
Return false;
}
}
}
}