Unexpected output when reading and writing to a text file

I'm a little new to C # files and I have a problem. When reading from a file and copying to another, the last piece of text is not written. Below is my code:

StringBuilder sb = new StringBuilder(8192); string fileName = "C:...rest of path...inputFile.txt"; string outputFile = "C:...rest of path...outputFile.txt"; using (StreamReader reader = File.OpenText(fileName)) { char[] buffer = new char[8192]; while ((reader.ReadBlock(buffer, 0, buffer.Length)) != 0) { foreach (char c in buffer) { //do some function on char c... sb.Append(c); } using (StreamWriter writer = File.CreateText(outputFile)) { writer.Write(sb.ToString()); } } } 

My goal was to read and write to a text file in a buffered manner. What in Java I would achieve as follows:

 public void encrypt(File inputFile, File outputFile) throws IOException { BufferedReader infromfile = null; BufferedWriter outtofile = null; try { String key = getKeyfromFile(keyFile); if (key != null) { infromfile = new BufferedReader(new FileReader(inputFile)); outtofile = new BufferedWriter(new FileWriter(outputFile)); char[] buffer = new char[8192]; while ((infromfile.read(buffer, 0, buffer.length)) != -1) { String temptext = String.valueOf(buffer); //some changes to temptext are done outtofile.write(temptext); } } } catch (FileNotFoundException exc) { } // and all other possible exceptions } 

Could you help me identify the source of my problem?

If you think there is a better approach to getting buffered I / O with text files, I would appreciate your suggestion.

+4
source share
5 answers

There are a couple of "gotchas":

  • c cannot be changed (this is a foreach iteration variable), you will need to copy it to process before writing
  • you need to track the size of your buffer, ReadBlock fills it with characters that could make your file dirty

Changing your code like this looks like this:

 //extracted from your code foreach (char c in buffer) { if (c == (char)0) break; //GOTCHA #2: maybe you don't want NULL (ascii 0) characters in your output char d = c; //GOTCHA #1: you can't change 'c' // d = SomeProcessingHere(); sb.Append(d); } 
+1
source

Try the following:

  string fileName = @""; string outputfile = @""; StreamReader reader = File.OpenText(fileName); string texto = reader.ReadToEnd(); StreamWriter writer = new StreamWriter(outputfile); writer.Write(texto); writer.Flush(); writer.Close(); 
+1
source

If you don't care about carraign return, you can use File.ReadAllText

This method opens the file, reads each line of the file, and then adds each line as a line item. Then it closes the file. A line is defined as a sequence of characters followed by a carriage return ('\ r'), a line feed ('\ n'), or a carriage return followed by a line. The received line does not contain the final carriage return and / or line.

 StringBuilder sb = new StringBuilder(8192); string fileName = "C:...rest of path...inputFile.txt"; string outputFile = "C:...rest of path...outputFile.txt"; // Open the file to read from. string readText = File.ReadAllText(fileName ); foreach (char c in readText) { // do something to c sb.Append(new_c); } // This text is added only once to the file, overwrite it if it exists File.WriteAllText(outputFile, sb.ToString()); 
0
source

Does this work for you?

  using (StreamReader reader = File.OpenText(fileName)) { char[] buffer = new char[8192]; bool eof = false; while (!eof) { int numBytes = (reader.ReadBlock(buffer, 0, buffer.Length)); if (numBytes>0) { using (StreamWriter writer = File.CreateText(outputFile)) { writer.Write(buffer, 0, numBytes); } } else { eof = true; } } } 

You still have to take care of character encoding!

0
source

If something is missing for me, it seems that your problem is that you are overwriting the existing contents of your output file at each iteration of the blockread.

You call:

  using (StreamWriter writer = File.CreateText(outputFile)) { writer.Write(sb.ToString()); } 

for each iteration of ReadBlock. The output file will be only the last piece of data that has been read.

From the MSDN documentation for File.CreateText:

If the file specified by the path does not exist, it is created. If the file exists, its contents are overwritten.

0
source

Source: https://habr.com/ru/post/1412993/


All Articles