意外的“,"从StreamReader到StreamWriter [英] Unexpected "," from StreamReader to StreamWriter

查看:58
本文介绍了意外的“,"从StreamReader到StreamWriter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了尝试和询问朋友外,我还尝试了很多事情,但是如果没有用,"代替第二行,我似乎无法写第二行.我要做的就是在单独的文件中为每一项读取一行.

I've tried a lot of things as well as research and asking friends, but can't seem to write a second line without a "," replacing the line. All I'd to do is have a single line for each item read in a separate file.

每个读取的文件都有以下几项:

Each read file has several of these items:

2/20/2014 7:33:10 AM
OPERATOR: jason
FILE: C:\ax14\Setups\000062363106RH.prt
UNITS: english
TEST RESULT: Pass

    CHANNEL 1
        TEST TYPE: VELOCITY
        RESULT:  Pass
        UPPER LIMIT: 0.2260
        LOWER LIMIT: 0.2220
        MAX THICKNESS: 2.0110
        MIN THICKNESS: 1.0110
        MEASURED VELOCITY: 0.2225
        MEASURED THICKNESS: 1.5215

Id喜欢将日期和速度线放在一行中,如下所示: "2014年2月20日上午7:33:10,测得的速度:0.2225"

Id like to have the date and velocity line in one line like this: "2/20/2014 7:33:10 AM, MEASURED VELOCITY: 0.2225"

这是我的问题

2/20/2014 7:33:10 AM,
,
,
,
,
,
,
,
,
,
,
,
,   MEASURED VELOCITY: 0.2225
,
2/20/2014 7:52:28 AM,
,
,
,
,
,
,
,
,
,
,
,
,   MEASURED VELOCITY: 0.2224
,
2/20/2014 7:58:46 AM,


   using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

namespace conApp
{
    class Program
    {

        static void Main(string[] args)
        {
            String line;
            try
            {
                using (StreamWriter sw = new StreamWriter("C:\\writetest\\writetest.txt"))
                {
                    string mydirpath = "C:\\chat\\";

                    string[] txtFileList = Directory.GetFiles(mydirpath, "*.txt");

                    foreach (string txtName in txtFileList)
                    {
                        System.IO.StreamReader sr = new System.IO.StreamReader(txtName);

                            while ((line = sr.ReadLine()) != null)
                            {
                                if (!string.IsNullOrEmpty(line))
                                {
                                    String spart = ".prt";
                                    String sam = " AM";
                                    String spm = " PM";
                                    String sresult = "TEST RESULT: ";
                                    String svelocity = "MEASURED VELOCITY: ";
                                    String part = "";
                                    String date = "";
                                    String result = "";
                                    String velocity = "";
                                    // sw.WriteLine(line);

                                    if (line.Contains(sam))
                                    {
                                        date = line;
                                    }

                                    if (line.Contains(spm))
                                    {
                                        date = line;
                                    }

                                    if (line.Contains(spart))
                                    {
                                        part = line;
                                    }

                                    if (line.Contains(sresult))
                                    {
                                        result = line;
                                    }

                                    if (line.Contains(svelocity))
                                    {
                                        velocity = line;
                                    }
                                    int I = 2;
                                    string[] x = new string[I];
                                    x[0] = date;
                                    x[1] = velocity;

                                    sw.WriteLine(x[0] + "," + x[1]);
                                }


                            }

                    }
                }
            }
            catch
            {

            }
        }
    }
}

推荐答案

以下是我对完整Main()的建议,即尝试从您的代码中尽可能多地使用.在while语句之外声明您的var,无需将其设置为空.

Here is my suggestion for the full Main() trying to use as much from your code as possible. Declaring your vars outside the while statement you don't need to make it null.

编辑-我忘了你说的话:

每个读取的文件都有其中几个项目

Each read file has several of these items

因此添加了几行内容来处理该问题.

So added a few lines to handle that.

static void Main(string[] args)
{
    string line;
    try
    {
        using (StreamWriter sw = new StreamWriter("C:\\writetest\\writetest.txt"))
        {
            string mydirpath = "C:\\chat\\";

            string[] txtFileList = Directory.GetFiles(mydirpath, "*.txt");

            foreach (string txtName in txtFileList)
            {
                string spart = ".prt";
                string sam = " AM";
                string spm = " PM";
                string sresult = "TEST RESULT: ";
                string svelocity = "MEASURED VELOCITY: ";
                string part = string.Empty;
                string date = string.Empty;
                string result = string.Empty;
                string velocity = string.Empty;

                using (StreamReader sr = new StreamReader(txtName))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (!string.IsNullOrEmpty(line) && line.Trim().Length != 0)
                        {
                            if (line.Contains(sam) || line.Contains(spm))
                            {
                                // Every new date means a new record. If you already have data for a record, first write it.
                                if (date != string.Empty && velocity != string.Empty)
                                {
                                    int I = 2;
                                    string[] x = new string[I];
                                    x[0] = date;
                                    x[1] = velocity;
                                    sw.WriteLine(x[0] + "," + x[1]);
                                }
                                // Then reset data to prepare it for a new record
                                part = string.Empty;
                                result = string.Empty;
                                velocity = string.Empty;
                                date = line;
                            }

                            if (line.Contains(spart))
                            {
                                part = line;
                            }

                            if (line.Contains(sresult))
                            {
                                result = line;
                            }

                            if (line.Contains(svelocity))
                            {
                                velocity = line;
                            }
                        }
                    }
                }

                // After last record you still have some data to write
                if (date != string.Empty && velocity != string.Empty)
                {
                    int I = 2;
                    string[] x = new string[I];
                    x[0] = date;
                    x[1] = velocity;
                    sw.WriteLine(x[0] + "," + x[1]);
                }
            }
        }
    }
    catch
    {

    }
}

这篇关于意外的“,"从StreamReader到StreamWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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