Save the File in a single text file and also in the path i specified
Hi all i have done a code to save my file as follows
if (m_strStandardEntryClassCode == "PPD")
{
m_strPath += "/PPD_BatchHeader_" + m_strDate + ".txt";
}
else
{
m_strPath += "/CCD_BatchHeader_" + m_strDate + ".txt";
}
using (TextWriter tw = new StreamWriter(m_strPath))
{
tw.Write(m_strRecordTypeCode.PadLeft(1, '0'));
tw.Write(m_strServiceClassCode.PadLeft(3,开发者_Go百科 '0'));
tw.Write(m_strCompanyName.PadRight(16, ' '));
tw.Write(m_strCompanyDiscretionaryData.PadRight(20, ' '));
tw.Write(m_strCompanyIdentification.PadRight(10, ' '));
tw.Write(m_strStandardEntryClassCode.PadRight(3, ' '));
tw.Write(m_strCompanyEntryDescription.PadRight(10, ' '));
tw.Write(m_strCompanyDescriptiveDate.PadLeft(6, '0'));
string m_strEffDate = m_strEffectiveEntryDate.Replace("/", "");
tw.Write(m_strEffDate.PadLeft(6, '0'));
tw.Write(m_strOriginatorStatusCode.PadRight(1, ' '));
tw.Write(m_strOriginationDFIIdentification.PadLeft(8, '0'));
tw.Write(m_strBatchNumber.PadLeft(7, '0'));
tw.Flush();
tw.Close();
}
Now i would like to save those two files in to single file and also as a multiple one. Can any one tell how to do this...
You'd want something like this.
Your data looks the same each time (or at least I can't see how the data differs) but I'm sure that you'll get the gist.
You need to use File.Append to add new data to a file and File.Open to clear and write new data. Call MergedDataWrite twice. If you need to write several files to it.
public void SeparateDataWrite()
{
if (m_strStandardEntryClassCode == "PPD")
{
m_strPath += "/PPD_BatchHeader_" + m_strDate + ".txt";
}
else
{
m_strPath += "/CCD_BatchHeader_" + m_strDate + ".txt";
}
using (StreamWriter w = File.Open(m_strPath, FileMode.Create)
{
WriteData(w);
w.close();
}
}
public MergedDataWrite()
{
using (StreamWriter w = File.Append("somefilename.txt")
{
WriteData(w);
w.Close();
}
}
public void WriteData(TextWriter tw)
{
tw.Write(m_strRecordTypeCode.PadLeft(1, '0'));
tw.Write(m_strServiceClassCode.PadLeft(3, '0'));
tw.Write(m_strCompanyName.PadRight(16, ' '));
tw.Write(m_strCompanyDiscretionaryData.PadRight(20, ' '));
tw.Write(m_strCompanyIdentification.PadRight(10, ' '));
tw.Write(m_strStandardEntryClassCode.PadRight(3, ' '));
tw.Write(m_strCompanyEntryDescription.PadRight(10, ' '));
tw.Write(m_strCompanyDescriptiveDate.PadLeft(6, '0'));
string m_strEffDate = m_strEffectiveEntryDate.Replace("/", "");
tw.Write(m_strEffDate.PadLeft(6, '0'));
tw.Write(m_strOriginatorStatusCode.PadRight(1, ' '));
tw.Write(m_strOriginationDFIIdentification.PadLeft(8, '0'));
tw.Write(m_strBatchNumber.PadLeft(7, '0'));
tw.Flush();
}
Various ways you can do it, I'd suggest.
- Copy file number 1 to a file called what the single combined file should be called.
- Open the new file (the single combined file) for appending.
- Open file number 2 for reading and read in all the contents.
- Write the data you've just read in to the single combined file.
This link has a simple code sample for doing something similar.
Another option would be to store all the data in memory and then write the 3 files straight from memory.
精彩评论