zoukankan      html  css  js  c++  java
  • 二进制和纯文本数据的读写

    引用别人的,这里做个纪录

    using System;
    using System.IO;
    using System.Text;
    
    namespace ConsoleApplication6
    {
        class Program
        {
            public static void Main(string[] args)
            {
                double[,] r = { { 2, 3 }, { 4, 5 } };
    
                //文本方式
                using (StreamWriter sw = new StreamWriter("1.txt", false))
                {
                    foreach (double d in r)
                    {
                        sw.Write(d);
                        sw.Write(" ");
                    }
                }
                string[] temp = File.ReadAllText("1.txt", Encoding.Default).Split(new char[] { ' ' });
                int i = 0;
                for (int y = 0; y < 2; y++)
                    for (int x = 0; x < 2; x++)
                    {
                        r[y, x] = Double.Parse(temp[i]);
                        i++;
                        
                    }
                Console.WriteLine("");
                //二进制方式
                using (FileStream fs = new FileStream("1.bin", FileMode.Create, FileAccess.Write))
                {
                    using (BinaryWriter bw = new BinaryWriter(fs))
                    {
                        foreach (double d in r)
                        {
                            bw.Write(d);
                        }
                    }
                }
                using (FileStream fs = new FileStream("1.bin", FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        for (int y = 0; y < 2; y++)
                            for (int x = 0; x < 2; x++)
                            {
                                r[y, x] = br.ReadDouble();
                            }
                    }
                }
            }
        }
    }
    

      

  • 相关阅读:
    双向链表

    一个简单makefile
    内存管理
    队列
    postgresql表结构查询sql
    postgresql 命令
    NSMutableDictionary 中使用setValueForKey 和 setObjectForKey有什么区别?
    ViewController 生命周期
    程序启动过程
  • 原文地址:https://www.cnblogs.com/liq07lzucn/p/6236513.html
Copyright © 2011-2022 走看看