I need to add this code to an action listener. I need it to work with my Jbutton (Reverse) When trying to add the action to reverse it does not work at all sends back multiple errors. The code works perfect by itself just not inside the action listener I used:
import java.io.FileReader;
import java.io.BufferedReader
import java.io.FileNotFoundException;
import java.util.*;
class FileReaderReverse
{
public static void main(String[] args) throws IOException,FileNotFoundException
{
FileReader fr=new FileReader("input.txt");
BufferedReader br=new BufferedReader(fr);
String s;
List<String>
tmp = new ArrayList<String>();
do{
s = br.readLine();
tmp.add(s);
}while(s!=null);
for(int i=tmp.size()-1;i>=0;i--) {
System.out.println(tmp.get(i));
}
}
}
I tried to do it like this and it didn't work. This is how I did the rest of my code while inputting a file. Doesn't seem to work. I have buttons added. I am trying to add it to my jButton (Reverse).
public void actionPerformed(ActionEvent e) {
try {
FileReader fr=new FileReader("input.txt");
BufferedReader br=new BufferedReader(fr);
String s;
List<String> tmp = new ArrayList<String>();
do{
s = br.readLine();
tmp.add(s);
}while(s!=null);
for(int i=tmp.size()-1;i>=0;i--) {
System.out.println(tmp.get(i));
} catch (Exception exp) { exp.printStackTrace();
} finally {
try {
reader.close();
} catch (Exception exp) {
}
}
}