Question

In: Computer Science

Using java, I need to make a program that reverses a file. The program will read...

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()
    {
  
    }
  
}

Solutions

Expert Solution

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:

  • Don't push file name to stack object as you did in main method.
  • Instead pass the stack object to readfile method and push each character of file to the stack.
  • Call reverseFile() method and pop each character and print it to output file.

###################################################

CODE :

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;

public class StackReverser {

public static void main(String[] args) throws IOException
{
//Create stack object
Stack myStack = new Stack();
//Create current class object
StackReverser obj = new StackReverser();
//Call readFile and reverseFile methods one-by-one by passing stack as argument
obj.readFile(myStack);   
obj.reverseFile(myStack);
  
System.out.println();
}

private void readFile(Stack myStack) throws FileNotFoundException
{
//Required variables
String fileName = "input.txt";
String line;
//Create file object and open file with scanner objetc
File file;
file = new File(fileName);
Scanner fileScanner;
fileScanner = new Scanner(file);
  
//For eachline in the file
while(fileScanner.hasNextLine())
{
//Store the lineto 'line' variable
line = fileScanner.nextLine();
//Push each character of the line to the stack
for(int i = 0;i<line.length();i++){
myStack.push(line.charAt(i));
}
}
}
private void reverseFile(Stack myStack) throws IOException
{
String fileName = "output.txt";
String line;
//Create file object and open file with BufferedWriter objetc
File file;
file = new File(fileName);
BufferedWriter fileWriter;
fileWriter = new BufferedWriter(new FileWriter(file));
  
//Print each character from the top of the file to new text file and pop it out from the stack
while(!myStack.isEmpty()){
fileWriter.write(myStack.peek().toString());
myStack.pop();
}
fileWriter.flush();
fileWriter.close();
}
}

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 :)


Related Solutions

I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
I need to make JAVA program that calculates the area of a brick of a specific...
I need to make JAVA program that calculates the area of a brick of a specific color. I have class Brick with the following UML: String color int number int length int width I made methods: getColor(), getNumber(), getLength(), getWidth() in the class Brick So it starts like this: ##### public Brick(String color, int number, int length, int width) { this.color = color; this.number = number; this.length = length; this.width = width;    }   public String toString() {   return number...
I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
I need C++ program that Read an input file of text.txt one word at a time....
I need C++ program that Read an input file of text.txt one word at a time. The file should consist of about 500 words. The program should remove all punctuations,keep only words. Store the words in a built-in STL container, such as vector or map.Can someone help with any additional comments that I can understand the logic?thank you
So I need to make a java program that reverse or replace first or lastchar or...
So I need to make a java program that reverse or replace first or lastchar or remove char from user's string input. length, concat, charAt, substring, and equals (or equalsIgnoreCase) thses are the string method that only I can use for example if user put asdf asdf and chose reverse input should be fdsa fdsa if user put asdf asdf and chose replace first a with b input should be bsdf asdf if user put asdf asdf and chose remove...
I need to update this java program to take input about each employee from a file...
I need to update this java program to take input about each employee from a file and write their information and salary/pay information to a file. use input file payroll.txt Kevin Yang 60 20 Trey Adams 30 15 Rick Johnson 45 10 Cynthia Wheeler 55 11.50 Sarah Davis 24 10 Muhammad Rabish 66 12 Dale Allen 11 18 Andrew Jimenez 80 15 import java.util.Scanner; public class SalaryCalcLoop { double Rpay = 0, Opay = 0; void calPay(double hours, double rate)...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Java Code Question: The program is supposed to read a file and then do a little...
Java Code Question: The program is supposed to read a file and then do a little formatting and produce a new txt file. I have that functionality down. My problem is that I also need to get my program to correctly identify if a file is empty, but so far I've been unable to. Here is my program in full: import java.io.*; import java.util.Scanner; public class H1_43 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT