
I need to read a file that was selected by the user using the JButton. Each line in the file then needs to be converted in Reverse Order to another text file. When that is done the output values of that file need to display in a JTextArea field. I have a good share of the code done and an area to display the names and path on the console. But I can not get the file to read line by line and output in reverse order to the text area field. Can someone help me understand where I am going wrong? Thank you and here is what I have so far:
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JButton; import javax.swing.JPanel; import java.io.BufferedReader; import java.io.BufferedWriter;
public class TextFileInput extends JFrame {
private JTextArea textAreaOutput = new JTextArea(20, 80);
//======================================================= constructor
public void TextFileInput() {
// Create a button to open a file
JButton openFileButton = new JButton("Open");
openFileButton.addActionListener(new OpenAction());
//... Create a "controls" panel with button on it.
JPanel controls = new JPanel();
controls.setLayout(new FlowLayout());
controls.add(openFileButton);
//... Create the content pane with controls and a textarea.
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(controls, BorderLayout.NORTH);
content.add(new JScrollPane(textAreaOutput), BorderLayout.CENTER);
this.setContentPane(content);
this.pack();
this.setTitle("File Input application");
}
////////////////////////////////////// inner listener class OpenAction
class OpenAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser(".");
int returnVal = fileChooser.showOpenDialog(TextFileInput.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File inFile = fileChooser.getSelectedFile();
try {
FileReader reader = new FileReader(inFile);
BufferedReader bufReader = new BufferedReader(reader);
String line = null;
// FileWrite copiedFile = new File("copiedfile.txt");
// BufferedWriter bufWrite = new BufferedWriter (new bufWrite(copiedFile));
while ((line = bufReader.readLine()) != null) {
textAreaOutput.append(line + "\n");
}
reader.close();
} catch (IOException ioex) {
System.err.println(ioex);
System.exit(1);
}
}
}//end actionPerformed
}//end inner listener class OpenAction
}
import java.io.*; import java.util.Scanner; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JFileChooser; import java.io.IOException;
public class FileDataInput {
public static void main(String[] args) {
JFrame window = new FileInput();
window.setSize(600, 400);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
JFileChooser chooser = new JFileChooser();
FileOutputStream out;
PrintStream printStream;
File F = new File(".");
File NameDir, NamePath, FileName;
int Checker;
chooser.setCurrentDirectory(F);
chooser.setDialogTitle("Select a File");
Checker = chooser.showOpenDialog(null);
if (Checker == JFileChooser.APPROVE_OPTION) {
NameDir = chooser.getCurrentDirectory();
NamePath = chooser.getSelectedFile();
System.out.println("The name of directory:" + NameDir.getName());
System.out.println("The name of the path:" + NamePath.getAbsolutePath());
}
else {
JOptionPane.showMessageDialog(null, "You have Cancelled", "Cancel Dialog Box", JOptionPane.WARNING_MESSAGE);
}
}
//private javax.swing.JTextField textAreaOutput;
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.