zoukankan      html  css  js  c++  java
  • 向文件写入文本

    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")) 
            {
                //天剑一些文本
                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();
            }
        }
    }



  • 相关阅读:
    书单
    树莓派与 NATAPP 实现内网穿透
    WinForm分辨率适应-高DPI自动缩放
    ElasticSearch学习——搜索技术基础知识(上)
    JavaSE学习笔记-基础
    JavaSE学习笔记-第一个Java程序
    JavaSE学习笔记-Java开发环境搭建
    MySQL学习笔记-增删改查
    MySQL学习笔记-函数
    MySQL学习笔记-查询
  • 原文地址:https://www.cnblogs.com/dengshiwei/p/4258687.html
Copyright © 2011-2022 走看看