Question

In: Computer Science

Java Code using Stack Write a program that opens a text file and reads its contents...

Java Code using Stack

Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file.

Ex input file:

Good Morning!

Bye..

EX output file:

!gninroM dooG

...eyB

Solutions

Expert Solution

import java.io.*;
import java.util.*;
class Stack{
    private static int top,maxsize;   
    private static char arr[];
        //Stack creation using arrays.
    Stack(int n){ 
                this.maxsize=n;
        top=-1;         
                arr=new char[maxsize];
    }   
        //push character into stack
    void push(char p){  
        char val=p;  
        top++;  
        arr[top]=val;   
    }   
    //pop character from stack  
    char pop(){   
        char o=arr[top];
        top --;    
        return o;  
    }
} 
public class File1{
        public static void main (String [] args)throws IOException{
                FileReader f=new FileReader("input.txt");
        BufferedReader read=new BufferedReader(f);
        String data;
                try{
                        //creating output file.
                        File fi=new File("output.txt");
                        //if output file exists then it is deleted and creates new one.
                        if(fi.exists()){
                                fi.delete();
                        }
                        FileWriter d=new FileWriter("output.txt",true);
                        while((data=read.readLine())!=null){
                                Stack s=new Stack(data.length());
                                //pushing characters into stack.
                                for(int i=0;i<=data.length()-1;i++){
                                        s.push(data.charAt(i)); 
                                }
                                //writing characters into output file.
                                for(int i=0;i<data.length();i++){
                                        d.write(s.pop());
                                }
                                d.write("\n");
                        }
                        //closing output file to ensure security.
                        d.close();
                }catch(Exception e){System.out.println(e);}
        }
}

Note:- If your input file is in another location i.e., other than current working directory, then please specify path in the above code as follows:-

FileReader f=new FileReader("C:\\Users\\HP\\Desktop\\java\\input.txt");


Related Solutions

Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
Write a program that reverses a text file by using a stack. The user interface must...
Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
Write a program in Java that reads an input text file (named: input.txt) that has several...
Write a program in Java that reads an input text file (named: input.txt) that has several lines of text and put those line in a data structure, make all lowercase letters to uppercase and all uppercase letters to lowercase and writes the new lines to a file (named: output.txt).
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Write a program that will write the contents of a text file backwards one character at...
Write a program that will write the contents of a text file backwards one character at a time. Your program should first ask for the text file name to be processed – and after verifying that the file exists, it should read the file into a StringBuilder buffer. Save the new file using the old filename appended with “Ver2-“ at the front. So for the file “MyStuff.txt” the file would be saved as Ver2-MyStuff.txt”. You can use any text file...
Write a Java program that reads words from a text file and displays all the non-duplicate...
Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT