C # file operations (beginner)

Source: Internet
Author: User

It is relatively simple and suitable for beginners.
Question:
First, create a file and write the content entered by the keyboard to the file. Then, read and write the file in text and binary mode to obtain the file attributes.
Procedure
1. Create a console program: Open vs2010, File-> new-> project, select console application, and write the desired name in the name. My name is File.
2. Code Writing steps:
1) create a FileStream object and define the stream pointing to the file;
2) create a streamwriter object and write the content entered from the console to the created file. When the input is 0, the input ends;
3) create a streamreader object, read the content of the newly created file in text mode, and output it to the console;
4) create a binaryreader object, read the characters one by one, and convert them into ascii codes;
5) create a fileInfo object to obtain attributes such as the file name and length;
3. The specific code is as follows: (the code is explained in detail)
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. IO;
Namespace File
{
Class Program
{
Static void Main (string [] args)
{Www.2cto.com
// Unlike creating a FileStream object, creating a StreamWriter object does not provide a set of similar options:
// Except adding a Boolean value to the end of the file or creating a new file, there is no such thing as the FileStream class.
// Specifies the option of the FileMode attribute. In addition, there is no option to set the FileAccess attribute, so there is always the read/write permission on the file.
// To use advanced parameters, you must specify these parameters in the FileStream constructor and create StreamWriter in the FileStream object.
// StreamWriter sw = new StreamWriter (@ "C: \ file .txt", true );
// This constructor has two parameters: one is the file name and the other is a Boolean value. This Boolean value specifies the method for creating an object as follows:
// If this value is false, a new file is created. If the original file exists, it is overwritten.
// If this value is true, open the file and retain the original data. If the file cannot be found, create a new file.
Try
{
FileStream fs = new FileStream (@ "C: \ file .txt", FileMode. OpenOrCreate, FileAccess. ReadWrite );
StreamWriter sw = new StreamWriter (fs );
String str = Console. ReadLine (); // obtain the input content on the keyboard and write it to the file.
While (str! = "0") // when the input is 0, the input ends.
{
Sw. WriteLine (str );
Str = Console. ReadLine ();
}
Sw. Close ();
}
Catch (IOException ex)
{
Console. WriteLine (ex. Message );
Console. ReadLine ();
Return;
}
Try
{
FileStream fs = new FileStream (@ "C: \ file .txt", FileMode. OpenOrCreate, FileAccess. ReadWrite );
StreamReader sr = new StreamReader (fs );
Console. writeLine ***************** ***************");
String strs = sr. ReadLine ();
While (strs! = Null)
{
Console. WriteLine (strs );
Strs = sr. ReadLine ();
}
Sr. Close ();
}
Catch (IOException ex)
{
Console. WriteLine (ex. Message );
Return;
}
Console. WriteLine ();
Try
{
Console. writeLine ("**************** read files in binary mode ***************** ***************");
FileStream fs = new FileStream (@ "C: \ file .txt", FileMode. OpenOrCreate, FileAccess. ReadWrite );
BinaryReader br = new BinaryReader (fs );
While (br. BaseStream. Position <br. BaseStream. Length)
{
Char ch = br. ReadChar (); // obtain each character in the character
Int I = Convert. ToInt32 (ch); // obtain the character ASCII code
If (I = 10) // the ASCII code is 10, which is the line feed key.
{
Continue;
}
If (I = 13) // the ASCII code is 13, which is the return key.
{
Console. WriteLine ();
Continue;
}
Console. Write (Convert. ToInt32 (ch ));
If (I! = 13 & I! = 10)
Console. Write (".");

}
Br. Close ();
Fs. Close ();
}
Catch (IOException ex)
{
Console. WriteLine (ex. Message );
Return;
}
Console. WriteLine ();
Console. WriteLine ();
Console. writeLine ******************* *************");
FileInfo fileInfo = new FileInfo (@ "C: \ file .txt ");
Console. WriteLine ("file Name:" + fileInfo. Name );
Console. WriteLine ("file name <including path>:" + fileInfo. FullName );
Console. WriteLine ("File Size <byte>:" + fileInfo. Length );
Console. WriteLine ("File Creation Time:" + fileInfo. CreationTime );
}
}
}
The program running effect is as follows:

Related Article

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.