In: Computer Science
Using java, I need to make a program that reverses a file.
The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order)
EX: Hello World eyB
Bye becomes dlroW olleH
I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and then reversing it
public class StackReverser {
public static void main(String[] args) throws
IOException
{
Stack myStack = new Stack();
myStack.push(fileName);
System.out.println(myStack.peek());
System.out.println(myStack.pop());
}
private void readFile() throws
FileNotFoundException
{
String fileName =
"input.txt";
File file;
file = new File(fileName);
Scanner
fileScanner;
fileScanner = new Scanner(file);
while(fileScanner.hasNextLine())
{
String line = fileScanner.nextLine();
}
}
private void reverseFile()
{
}
}
Here is the answer for your question in Java Programming Language.
Kindly upvote if you find the answer helpful.
How to reverse the file content with stack:
###################################################
CODE :
import java.io.BufferedWriter; public class StackReverser { public static void main(String[] args) throws
IOException private void readFile(Stack myStack) throws
FileNotFoundException |
input.txt
Hello World eyB |
#########################################################
SCREENSHOTS :
Please see the screenshots of the code below for the indentations of the code.
input.txt
#########################################################
OUTPUT :
output.txt file
Any doubts regarding this can be explained with pleasure :)