. net file reads and writes to files

Source: Internet
Author: User

    1. The steps to read and write files typically require 5 steps:
    2. Create a file stream
    3. Create Reader/writer
    4. Perform a read or write operation
    5. Turn off reader/writer
    6. Close file stream

Reference Required: System.IO this namespace

Code Demo:

1 stringPath =@"F:\a.txt";2 stringContent ="content";3  Try4 {5     //Create a file stream6FileStream fs =NewFileStream (path, filemode.create);7     //Create a writer to stream files into the writer8StreamWriter W =NewStreamWriter (FS);9     //Perform ActionsTen w.write (content); One     //Close the writer A w.close (); -     //Close File Stream - FS. Close (); theConsole.WriteLine ("Write Success! "); - } - Catch(Exception ex) - { +Console.WriteLine ("An error has occurred! "+Ex. Message); -}

An exception is thrown when the path parameter of the FileStream is empty

Common values for FileMode enumerations are:

    1. Create: Creates a new file with the specified name. If the file exists, overwrite the old file.
    2. CreateNew: Create a new file. If the file exists, an exception occurs and the hint file already exists.
    3. Open: Opens a file. When using this enumeration value, the specified file must exist, otherwise an exception will occur.
    4. Openorcreate:openorcreate is similar to the open member, except that if the file does not exist, create a new file with the specified name and open it
    5. Append: Opens an existing file and appends content to the end of the file.
    6. Other visible official help documents.

You can specify the encoding enumeration type when StreamWriter constructs encoding

The values of common encoding are:

    1. Default: Operating system defaults encoding
    2. ASCII: American Standard Code for Information interchange for use in a purely English environment
    3. Utf8:utf-8 format encoding
    4. Unicode: An encoding scheme that accommodates all the characters in the world, with the disadvantage of being a large space-occupying

Other encodings can use encoding's static method GetEncoding (string name) to specify the character encoding, which is, of course, the encoding name that C # can support

Gives a C # supported encoding Encyclopedia: (getencodings () traversal)

IBM037
IBM437
IBM500
ASMO-708
DOS-720
ibm737
ibm775
ibm850
ibm852
IBM855
ibm857
IBM00858
IBM860
ibm861
DOS-862
IBM863
IBM864
IBM865
cp866
ibm869
IBM870
windows-874
cp875
Shift_JIS
gb2312
ks_c_5601-1987
Big5
IBM1026
IBM01047
IBM01140
IBM01141
IBM01142
IBM01143
IBM01144
IBM01145
IBM01146
IBM01147
IBM01148
IBM01149
Utf-16
Utf-16be
windows-1250
windows-1251
Windows-1252
windows-1253
windows-1254
windows-1255
windows-1256
windows-1257
windows-1258
Johab
Macintosh
X-mac-japanese
X-mac-chinesetrad
X-mac-korean
X-mac-arabic
X-mac-hebrew
X-mac-greek
X-mac-cyrillic
X-mac-chinesesimp
X-mac-romanian
X-mac-ukrainian
X-mac-thai
X-mac-ce
X-mac-icelandic
X-mac-turkish
X-mac-croatian
Utf-32
Utf-32be
X-chinese-cns
x-cp20001
X-chinese-eten
x-cp20003
x-cp20004
x-cp20005
X-ia5
X-ia5-german
X-ia5-swedish
X-ia5-norwegian
Us-ascii
x-cp20261
x-cp20269
IBM273
IBM277
IBM278
IBM280
IBM284
IBM285
IBM290
IBM297
IBM420
IBM423
IBM424
x-ebcdic-koreanextended
Ibm-thai
Koi8-r
IBM871
IBM880
IBM905
IBM00924
Euc-jp
x-cp20936
x-cp20949
cp1025
Koi8-u
Iso-8859-1
Iso-8859-2
Iso-8859-3
Iso-8859-4
Iso-8859-5
Iso-8859-6
Iso-8859-7
Iso-8859-8
Iso-8859-9
Iso-8859-13
Iso-8859-15
X-europa
Iso-8859-8-i
Iso-2022-jp
Csiso2022jp
Iso-2022-jp
Iso-2022-kr
x-cp50227
Euc-jp
Euc-cn
Euc-kr
hz-gb-2312
GB18030
X-iscii-de
x-iscii-be
X-iscii-ta
X-iscii-te
X-iscii-as
X-iscii-or
X-iscii-ka
X-iscii-ma
X-iscii-gu
X-iscii-pa
Utf-7
Utf-8

The read process for the----file is the same as the write

Not much difference in creating reader (replace 7-12 lines above, modify FileMode as appropriate)

1  // Create Reader 2 New StreamReader (FS); 3 // read content to variable 4 string content=reader. ReadToEnd (); 5 // Close Reader 6 Reader. Close ();

Want to help small partners who are learning file operations

. net file reads and writes to files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.