- What does in.read() do? This opens the apple.txt document and reads the contents.
No, it reads one character from the input stream.
- What does the line "sb.reverse().toString()" do? This reverses the first line of the document. Not exactly what I want, but if I can reverse the first line, it would be a minor success. I thought if I placed it after the in.read command it would store the reverse in the buffer and then use the next line to write it to the output file.
With the line "StringBuffer sb = new StringBuffer(br.readLine());" you are reading the first line of the file. Calling "sb.reverse()" reverses the characters in the first line.
Inside the while loop, the line "sb.reverse().toString();" reverses the content of the StringBuffer again, converts the content to a
String and then throws away the String (because you're not assigning the return value to a variable).
Nowhere in your program you're reading the input file line by line, so you keep reversing the first line, which is still in the StringBuffer, with each iteration of the loop.
- What are you writing to the output file in the line "out.write(c)"? The contents of the apple.txt document
No, you are just writing the character that you read in the line "while ((c=in.read()) != -1){" to the output file. So you're just copying the input file to the output file, character by character.
By the way, what was it that your program should do? Should it reverse each line (e.g., if line 1 of the input file contains "jesper" should it write "repsej" in line 1 of the output file), or should it just reverse the order of the lines? The last piece of code you posted is doing the second.
Why do you have these lines in your program:
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
You're not using in and out anywhere in your program.