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...
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...
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...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
Write a java program that can create, read, and append a file. Assume that all data...
Write a java program that can create, read, and append a file. Assume that all data written to the file are string type. One driver class and a class that contains the methods. The program should have three methods as follows: CreateFile - only creating a file. Once it create a file successfully, it notifies to the user that it creates a file successfully. ReadingFile - It reads a contents of the file. This method only allow to read a...
using java program I need only the tracing in detail, using my name "Nada Hamdan" Part-A:...
using java program I need only the tracing in detail, using my name "Nada Hamdan" Part-A: Consider first ten letters of your name and for each letter encode with an integer number. For example: If “Ahmed Naser” are the first ten letters from your name, then the array of integers will be {0, 7, 12, 4, 3,13,0,18,4,17}. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT