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) {
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);
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.
source share