zoukankan      html  css  js  c++  java
  • C#.NET向现有文件添加文本+创建一个新文本文件并写入一个字符串

    向现有文件添加文本:




    using
    System; using System.IO; class Test { public static void Main() { // Create an instance of StreamWriter to write text to a file. // The using statement also closes the StreamWriter. using (StreamWriter sw = new StreamWriter("TestFile.txt")) { // Add some text to the file. sw.Write("This is the "); sw.WriteLine("header for the file."); sw.WriteLine("-------------------"); // Arbitrary objects can also be written to the file. sw.Write("The date is: "); sw.WriteLine(DateTime.Now); } } }
    创建一个新文本文件并写入一个字符串:
    using System;
    using System.IO;
    public class TextToFile
    {
    private const string FILE_NAME = "MyFile.txt";
    public static void Main(String[] args)
    {
    if (File.Exists(FILE_NAME)) // 确认文件是否存在.
    {
    Console.WriteLine("{0} already exists.", FILE_NAME);
    return;
    }
    using (StreamWriter sw = File.CreateText(FILE_NAME))
    {
    sw.WriteLine ("This is my file.");
    sw.WriteLine ("I can write ints {0} or floats {1}, and so on.",
    1, 4.2);
    sw.Close();
    }
    }
    }
    
  • 相关阅读:
    JS常见错误和分析
    angularjs 笔记(1) -- 引导
    各大浏览器hack
    AngularJS 配置和运行phonecat错误
    npm start 作用
    $.prop()和$.attr() 区别用法
    HDU 1251 统计难题 (Tire树)
    Luogu P3370 【模板】字符串哈希
    Luogu P3385 【模板】负环
    LuoguP1563 玩具谜题
  • 原文地址:https://www.cnblogs.com/tdalcn/p/691298.html
Copyright © 2011-2022 走看看