Copy text file
I am trying to copy a text file to another text file line by line. There seems to be a 1024 character buffer. If my file has less than 1024 characters, my function won't copy to another file.
In addition, if the number of characters is more than 1024 characters but less than 1024, those exceeding characters will not be copied.
Example:
2048 characters in original file - 2048 copied
988 characters in original file - 0 copied
1256 characters in original file - 1024 copied
private void button3_Click(object sender, EventArgs e)
{
// écrire code pour reprendre le nom du fichier sélectionné et
//ajouter un suffix "_poly.txt"
string ma_ligne;
const int RMV_CARCT = 9;
//délcaration des fichier
FileStream apt_file = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
textBox1.Text = textBox1.Text.Replace(".txt", "_mod.txt");
FileStream mdi_file = new FileStream(textBox1.Text, FileMode.OpenOrCreate,FileAccess.ReadWrite);
//lecture/ecriture des fichiers en question
StreamReader apt = new StreamReader(apt_file);
StreamWriter mdi_line = new StreamWriter(mdi_file, System.Text.Encoding.UTF8, 16);
while (apt.Peek() >= 0)
{
ma_ligne = apt.ReadLine();
//if (ma_ligne.StartsWith("GOTO"))
//{
// ma_ligne = ma_ligne.Remove(0, RMV_CARCT);
// ma_ligne = ma_ligne.Replace(" ","");
// ma_ligne = ma_ligne.Replace(",", " ");
mdi_line.WriteLine(ma_ligne);
//}
}
apt_file.Close();
mdi_file.Close();
}
a source to share
Two questions:
- Classes
FileStream
,StreamWriter
andStreamReader
must be inside blocksusing { }
. They doIDisposable
, so you need to callDispose
and the blockusing
will do it for you. If you do this, that's really all you need to fix (which I'll explain in a minute). It also means you don't need to call anymoreClose()
. - At a minimum, call
mdi_line.Flush()
before closing it. This will immediately write the buffer to the file.
The call Dispose
in the class StreamWriter
will automatically call Flush
, so the block will using
fix the problem.
using (FileStream apt_file = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read))
{
textBox1.Text = textBox1.Text.Replace(".txt", "_mod.txt");
using (FileStream mdi_file = new FileStream(textBox1.Text, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
//lecture/ecriture des fichiers en question
using (StreamReader apt = new StreamReader(apt_file))
using (StreamWriter mdi_line = new StreamWriter(mdi_file, System.Text.Encoding.UTF8, 16))
{
while (apt.Peek() >= 0)
{
ma_ligne = apt.ReadLine();
//if (ma_ligne.StartsWith("GOTO"))
//{
// ma_ligne = ma_ligne.Remove(0, RMV_CARCT);
// ma_ligne = ma_ligne.Replace(" ","");
// ma_ligne = ma_ligne.Replace(",", " ");
mdi_line.WriteLine(ma_ligne);
//}
}
}
}
}
a source to share
Do you know there is a File.Copy () method? And FileInfo also has a Copy () method .
Edit: I see that you want to change the contents of the file while copying, although this is commented out for now.
For using StreamReader you can look at the msdn documentation . It offers an implementation without Peek()
:
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
a source to share
you need:
using (StreamReader apt= new StreamReader(apt_file))
{
while (apt.Peek() > -1){ ...}
}
see: http://msdn.microsoft.com/en-us/library/system.io.streamreader.peek.aspx
a source to share
You can read the file using the ReadLine () method like this:
using (StreamReader sr = new StreamReader(inputFile))
{
String line;
while ((line = sr.ReadLine()) != null)
{
// process line
}
}
a source to share