Write a program that reverses a text file by using a stack T

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 a new text file. At first, only the Read button is enabled. After it is clicked, it is disabled and the Reverse button is enabled. After it is clicked, it is disabled and the Write button is enabled. After it is clicked, it is disabled. The name of the input text file is \"input.txt\". The input text file will contain no more than 100 lines of text. This fact is not needed by the program. It simply means that memory usage is not an issue. The name of the output text file is \"output.txt\". Make sure you study the Final Project Guidance document and the Final Project Output document, which are located near the top of our course web site. Upload your zipped assignment folder, which contains the java and class files.

Solution

package www.kosoft.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

public class ReverseLineInputStream extends InputStream
{

RandomAccessFile in;

long currentLineStart = -1;
long currentLineEnd = -1;
long currentPos = -1;
long lastPosInFile = -1;

public ReverseLineInputStream(File file) throws FileNotFoundException
{
in = new RandomAccessFile(file, \"r\");
currentLineStart = file.length();
currentLineEnd = file.length();
lastPosInFile = file.length() -1;
currentPos = currentLineEnd;
}

public void findPrevLine() throws IOException
{

currentLineEnd = currentLineStart;


if (currentLineEnd == 0)
{
currentLineEnd = -1;
currentLineStart = -1;
currentPos = -1;
return;
}

long filePointer = currentLineStart -1;

while ( true) {
filePointer--;

// we are at start of file so this is the first line in the file.
if (filePointer < 0) {
break;
}

in.seek(filePointer);
int readByte = in.readByte();

if (readByte == 0xA && filePointer != lastPosInFile ) {   
break;
}
}
  
currentLineStart = filePointer + 1;
currentPos = currentLineStart;
}

public int read() throws IOException {

if (currentPos < currentLineEnd ) {
in.seek(currentPos++);
int readByte = in.readByte();
return readByte;

}
else if (currentPos < 0)
{
return -1;
}
else
{
findPrevLine();
return read();
}
}
}

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 t
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 t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site