Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

write reversed lines to a file  RSS feed

 
Dave Wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a file named apples.txt that has 3 sentences in it. The code below reads the first sentence and reverses it, but does not write the reversed sentence to the new output file. I want it to read and reverse all 3 sentences and then write the reversed sentences to the file revapples.txt. Any suggestions? Thanks.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look carefully at this part of your program:

Think about what the program is doing, statement by statement, and how that corresponds (or not) to what you want your program to do.

For example:
- What does in.read() do?
- What does the line "sb.reverse().toString()" do?
- What are you writing to the output file in the line "out.write(c)"?
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Javaranch, Lostin. We are a friendly bunch here, so we ask that you use your real name. As our Naming Policy states, accounts with obviously fictitious names will be closed.
You can change your displayed name here.
Thanks
 
Dave Wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the user name. I'm so used to using nicknames.
 
Dave Wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In answer to your questions:

- What does in.read() do? This opens the apple.txt document and reads the contents.
- 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.
- What are you writing to the output file in the line "out.write(c)"? The contents of the apple.txt document
 
Dave Wilson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. I tried an array and bufferedwriter and it works.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!