将2D uint-Array保存到txt文件 [英] Save 2D uint-Array to txt-File

查看:85
本文介绍了将2D uint-Array保存到txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我需要将uint [1280,959]数组保存到简单的txt文件中.

目前,我正在通过遍历数组将其转换为字符串[959]数组,然后使用File.WriteAllLines方法,请参见下文

Hey Guys,
I need to save a uint[1280,959] array to a simple txt-File.

Currently I''m converting the array to a string[959]-array by looping through it and then I use the File.WriteAllLines Method, see below

public void saveArray(uint[,] array, string savePath)
{
            int x, y;
            string[] saveData = new string[HEIGHT];          
            
            if (array != null)
            {
                try
                {                    
                    for (y = 0; y < HEIGHT; y++)
                    {
                        for (x = 0; x < WIDTH; x++)
                        {
                            saveData[y] += array[x, y] + "\t";
                        }
                    }
                                        
                    File.WriteAllLines(savePath, saveData);       
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                };
            }
}



问题是,此代码非常耗时(大约4-7秒),是所有转换的原因(实际保存过程很快).

有没有将uint [,]转换为字符串的更快方法,还是有另一种将2D数组保存到txt文件中的快速方法?

感谢您的帮助=)



The problem is, this code is very time consuming (about 4-7sec), all cause of the conversion (the actual saving process is fast).

Is there a faster way to convert uint[,] to string or is there another quick way to save an 2D-array into a txt-File?

Thanks for helping me =)

推荐答案

1)打开文件.
2)使用StringBuilder而不是字符串,并将其初始化为WIDTH * 2(一个为零,一个为制表符)
3)每行使用WriteLine(myStringBuilder.ToString()).
3)关闭并处置您的文件.
1) Open the file.
2) Use a StringBuilder rather than a string, and initilize it to WIDTH * 2 (one for a zero, one for a tab)
3) Use WriteLine(myStringBuilder.ToString()) for each line.
3) Close and Dispose your file.
public void saveArray(uint[,] array, string savePath)
    {
    int x, y;
    string[] saveData = new string[HEIGHT];
    using (TextWriter tw = new StreamWriter(savePath))
        {
        if (array != null)
            {
            try
                {
                for (y = 0; y < HEIGHT; y++)
                    {
                    StringBuilder sb = new StringBuilder(WIDTH * 2);
                    for (x = 0; x < WIDTH; x++)
                        {
                        sb.Append(array[x, y].ToString() + "\t");
                        }
                    tw.WriteLine(sb.ToString());
                    }
                }
            catch (Exception ex)
                {
                MessageBox.Show(ex.Message);
                }
            }
        }
    }


我做了一些小改动.它应该执行得更快一点.
I made some little changes. It should perform a little faster.
public static void SaveArray(uint[,] array, string path)
{
    if (array == null) return;
    string line;
    int width = array.GetLength(0);
    int height = array.GetLength(1);
    StreamWriter writer = new StreamWriter(path);
    try
    {
        for (int y = 0; y < height; y++)
        {
            line = string.Empty;
            for (int x = 0; x < width; x++)
            {
                line += array[x, y].ToString() + "\t";
            }
            writer.WriteLine(line);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
    finally
    {
        writer.Close();
    }
}


这篇关于将2D uint-Array保存到txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆