我想从文件中读取并在另一个文件中逐行写入. [英] I want read from the file and write line by line it in another file.

查看:81
本文介绍了我想从文件中读取并在另一个文件中逐行写入.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件中读取文件并将其逐行写入另一个文件中.
当然,第一个文件由长度为20的字符串代码组成,另一个文件为十六进制代码,我使用以下代码将字符串转换为十六进制:

I want read from the file and write line by line it in another file.
of course first file consist of string code with length 20 and another file is hex code and I convert string to hex with this code:

string hex = "";
                   foreach (char c in sline)
                   {
                       int tmp = c;
                       hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
                   }


sline是第一个文件中的字符串代码.


sline is string code in first file.
how can do this?

推荐答案

此处是示例代码.这可能是您需要做的很好的例子.
该代码未经测试!
Here is sample code. It may be good example of what you need to do.
That code is not tested!
void Foo(string inputFilePath, string outputFilePath)
        {

            try
            {
                if (File.Exists(inputFilePath))
                {
                    //open output stream
                    using (BinaryWriter bw = new BinaryWriter(File.Create(inputFilePath)))
                    {
                        //open input stream 
                        //!!! specify Encoding !!!
                        using (BinaryReader br = new BinaryReader(File.Open(inputFilePath, FileMode.Open, Encoding.ASCII)))
                        {

                            // Position and length variables.
                            int pos = 0;

                            // Use BaseStream.
                            int length = (int)br.BaseStream.Length;
                            while (pos < length)
                            {
                                var endOfLine = "\r\n"; // \n Or \r

                                // Read char and write char to output file
                                bw.Write(String.Format("0x{0:X}{1}", (int)br.ReadChar()), endOfLine);



                                // Advance our position variable.
                                pos += sizeof(int);
                            }
                        }
                    }
                }
                else
                {
                    throw new FileNotFoundException("Input file not found!",inputFilePath);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }

        }


;)


public void ConvertToHex(string inputFilePath, string outputFilePath)
        {
            using (StreamWriter writer = new StreamWriter(outputFilePath))
            {
                using (StreamReader reader = new StreamReader(inputFilePath))
                {
                    string text = reader.ReadToEnd();
                    foreach (char value in text)
                    {
                        writer.Write(string.Format("{0:x2}", (int)value));
                    }
                }
            }
        }


私有字符串HexAsciiConvert(string hex)

{

StringBuilder sb = new StringBuilder();

for(int i = 0; i< = hex.Length-2; i + = 2)

{

sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hex.Substring(i,2),

System.Globalization.NumberStyles.HexNumber)))));

}

返回sb.ToString();

}
private string HexAsciiConvert(string hex)

{

StringBuilder sb = new StringBuilder();

for (int i = 0; i <= hex.Length - 2; i += 2)

{

sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hex.Substring(i, 2),

System.Globalization.NumberStyles.HexNumber))));

}

return sb.ToString();

}


这篇关于我想从文件中读取并在另一个文件中逐行写入.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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