Friday, 28 June 2013

how to read the data from NOTEPAD or .txt file.How to split the .txt file and save in different .txt files in windows forms

                             Hi Friends in this article i will explain about how to read the data from NOTEPAD or .txt file.How to split the .txt file and save in different .txt files in windows forms.                             I already explained in the previous post how to save the data in notepad in VB.NET or C# windows forms and how to create notepad file or how to append the data to existing notepad file in VB.NET or C# windows forms.

 First of all,take one windows form.

File—New—Project or Ctrl+Shift+N

Take one notepad file that file contains below data.

  C1 24 48 3000 8 16 1 16 0 0 1
  C2 48 92 2500 8 16 1 16 0 0 2
  C3 48 59 2000 4 16 2 16 0 0 3

Now we split the data with space and save the data in different .txt files.
Double click on the form then you enter into the code file.
Write or copy and paste the following code.

VB.NET
Imports System.Windows.Forms
Imports System.IO
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer = 0

        ' Loop through each line in array returned by ReadAllLines
        Dim line As String
        For Each line In File.ReadAllLines("c:\temp\column.txt")

            ' Split line on comma
            Dim parts As String() = line.Split(" ")
            Dim swriter As StreamWriter
            swriter = File.AppendText("c:\temp\CID.txt")
            swriter.WriteLine(parts(0))
            swriter.Close()
            swriter = File.AppendText("c:\temp\Breath.txt")
            swriter.WriteLine(parts(1))
            swriter.Close()
            swriter = File.AppendText("c:\temp\Depth.txt")
            swriter.WriteLine(parts(2))
            swriter.Close()
            swriter = File.AppendText("c:\temp\AstReq.txt")
            swriter.WriteLine(parts(3))
            swriter.Close()
            swriter = File.AppendText("c:\temp\noofBars1.txt")
            swriter.WriteLine(parts(4))
            swriter.Close()
            swriter = File.AppendText("c:\temp\dia1.txt")
            swriter.WriteLine(parts(5))
            swriter.Close()
            swriter = File.AppendText("c:\temp\noofbars2.txt")
            swriter.WriteLine(parts(6))
            swriter.Close()
            swriter = File.AppendText("c:\temp\dia2.txt")
            swriter.WriteLine(parts(7))
            swriter.Close()
            swriter = File.AppendText("c:\temp\noofbars3.txt")
            swriter.WriteLine(parts(8))
            swriter.Close()
            swriter = File.AppendText("c:\temp\dia3.txt")
            swriter.WriteLine(parts(9))
            swriter.Close()
            swriter = File.AppendText("c:\temp\Pattern.txt")
            swriter.WriteLine(parts(10))
            swriter.Close()
            i += 1
        Next
    End Sub
End Class


 C#:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
Public Class Form1
{

     private void Form1_Load(System.Object sender, System.EventArgs e)
     {
          int i = 0;

          // Loop through each line in array returned by ReadAllLines
          string line = null;

          foreach (string line_loopVariable in File.ReadAllLines("c:\\temp\\column.txt")) {
              line = line_loopVariable;
              // Split line on comma
              string[] parts = line.Split(" ");
              StreamWriter swriter = null;
              swriter = File.AppendText("c:\\temp\\CID.txt");
              swriter.WriteLine(parts[0]);
              swriter.Close();
              swriter = File.AppendText("c:\\temp\\Breath.txt");
              swriter.WriteLine(parts[1]);
              swriter.Close();
              swriter = File.AppendText("c:\\temp\\Depth.txt");
              swriter.WriteLine(parts[2]);
              swriter.Close();
              swriter = File.AppendText("c:\\temp\\AstReq.txt");
              swriter.WriteLine(parts[3]);
              swriter.Close();
              swriter = File.AppendText("c:\\temp\\noofBars1.txt");
              swriter.WriteLine(parts[4]);
              swriter.Close();
              swriter = File.AppendText("c:\\temp\\dia1.txt");
              swriter.WriteLine(parts[5]);
              swriter.Close();
              swriter = File.AppendText("c:\\temp\\noofbars2.txt");
              swriter.WriteLine(parts[6]);
              swriter.Close();
              swriter = File.AppendText("c:\\temp\\dia2.txt");
              swriter.WriteLine(parts[7]);
              swriter.Close();
              swriter = File.AppendText("c:\\temp\\noofbars3.txt");
              swriter.WriteLine(parts[8]);
              swriter.Close();
              swriter = File.AppendText("c:\\temp\\dia3.txt");
              swriter.WriteLine(parts[9]);
              swriter.Close();
              swriter = File.AppendText("c:\\temp\\Pattern.txt");
              swriter.WriteLine(parts[10]);
              swriter.Close();
              i += 1;
          }
     }
    Public Form1()
     {
          Load += Form1_Load;
     }
}

Run the form with press F5.Then automatically the above .txt files created in the c:\\temp folder.

Happy Reading my Blog.

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.