Java Read Lines from Text File and Output in Reverse order to a Different Text File
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;
}
View Answers
Related Tutorials/Questions & Answers:
Read Lines from text fileRead Lines from text file Here's a brief desc of what my
Java code does .. I'm using BufferedReader to
read lines from a
text files and split each...
read from the
text file and displays the
output as desired. Unable to
read the rest
Advertisements
Java read lines from fileJava read lines from file Any code example related to
Java read lines from file? In my project there is requirement of reading the
file line by line... of reading
file line by line in
Java. Can any one share me the code for reading
Search and return lines of text fileSearch and return
lines of
text file I wrote the following code, that found
lines of txt
file with contains certain word, but it returns only the first or the last line! (However, the System.out.println show me all the
lines Read text FileRead text File Hi,How can I get line and keep in a String in
Java Steps to read text file in pysparkSteps to
read text file in pyspark Hi,
I am learning to write program in PySpark. I want to simply
read a
text file in Pyspark and then try some code.
What are the Steps to
read text file in pyspark?
How much time it takes
how to read a text file with scanner in javahow to
read a
text file with scanner in java Hi,
I am looking for the example code in
Java for reading
text file line by line using the Scanner class.
how to
read a
text file with scanner in
java?
Thanks
Hi
Java read text file text file in
Java?":
ADS_TO_REPLACE_3
Example of
Read text File Line... a
text file one line at a time. It can also
be used to
read large
text files... reads the number
from the
file. Here we have used hasNextLine()
method to
read Character from text file - Java BeginnersCharacter
from text file Write a program that reads characters
from a
text file. Your program will count how many time each character appear in the
text. Assume that the letters are case-sensitive.
Example
output Count characters from text file in Java is a
java code that count the occurrence of each character
from text file.
import...Count characters
from text file in Java At the "Count chracters fro mtext
file in
Java". I tried to run the code, but the error at the line have
Delete specific lines from text in JavaDelete specific
lines from text in Java Hi,
beginning in
java, i'm trying to delete specific line
from a
text file.
Ex: i want to delete data... number that i want to delete.
how could it be possible with
java.
thanks a lot
Read text file in PySparkRead text file in PySpark - How to
read a
text file in PySpark?
The PySpark...().setAppName("
read text file in pyspark")
sc = SparkContext(conf=conf... configuration
conf = SparkConf().setAppName("
read text file in pyspark")
sc
Reading a text file in java in java.io.* package for reading and writing to a
file in
Java.
To learn more about reading
text file in
Java see the tutorial
Read File in
Java.
Thanks...Reading a
text file in java What is the code for Reading a
text file How to read a large text file line by line in java?How to
read a large
text file line by line in
java? I have been assigned a work to
read big
text file and extract the data and save into database... you kind advice and let's know how to
read a large
text file line by line in
java Read from file javaRead from file java How to
Read from file java? What is the best method for a
text file having a size of 10GB. Since i have to process the
file one line at a time so tell me the very best method.
Thank you
How to read text file in Servlets
How to
read text file in Servlets
...
file in servlets.
In this example we will use the input stream to
read the
text from the disk
file. The InputStreamReader class is used to
read the
file how to read text file with java 8 stream apihow to
read text file with
java 8 stream api Hi,
I want to use
Java... code.
how to
read text file with
java 8 stream api?
Thanks
Hi,
Following example is for reading
text file line by line in
Java using the stream api
Read File from specified path in JavaHow to
read a
file from a specified path in
Java?
After learning so many... reads a
text file from specified path in
Java. We will
give complete path... the
file, the program will
read text data line by line and
print on the console
Read Specific Line from file Using JavaRead Specific Line
from file Using
Java
Here we are going to
read a specific line
from the
text file. For this we have created a for loop to
read lines 1 to 10
from the
text file. If the loop reached fifth line, the br.readLine() method
read text file and store the data in mysql - JDBCread text file and store the data in mysql when we store the data in mysql table
from text file its store the data
from new line to new column. how to store the data in
different column
from a single line of
text file.
how to read text file in jtable in netbeans7.0how to
read text file in jtable in netbeans7.0
text file... want to displaythe above .txt
file in jtable as following format having 3 columns
contigID length size
and then display sequence like "ATGCGSA..." in
text text filetext file Hello can I modify the program below so that all the numerical data is stored in an external
text file,that is the data contained... .in);
System.out.println("place your
order!");
System.out.print("NAME:");
String
text file at the start of the program
from a seperate external
text file.Thank you!
mport...
text file Hi can you help me I have to modify the program below so that all the data held in it is stored in an external
text file.So there should
Java search word from text fileJava search word
from text file
In this tutorial, you will learn how to search a word
from text file and
display data related to that word. Here, we have created a
text file student.txt
which consists of id, name and marks of few
binary search tree from text filebinary search tree
from text file How so I go about constructing a binary search tree
from a
text file, which has letters and numbers, which must be sorted and printed in ascending
order.
E.g.
Text file contents
3 apples pears
save text file - Java Beginnerssave
text file hi
i have just start programming in java.please guide me if i want to
read a
text file in java.then the
text file is save in which directory
Count characters from text file in JavaCount characters
from text file Using
Java
Here we are going to count the occurrence of each character
from the
text
file. For this, we have used the
BufferedReader class to
read the specified
file. Then we have
removed
Search word from text file using JavaHow to Search word
from text file using
Java
In this section, we are going to search a word
from the
text file. For this, we have created a swing button...
from the
text file.
Here is the code:
import java.io.*;
import
how to match the key word from a text filehow to match the key word
from a
text file p>Hi all,
I have the code to match the key word and
from the
text. I have input like this reader.txt... want to get the value
from the called
file and get the result.
String regex1
Adding a text file - Java BeginnersAdding a
text file Hello,
I need a program that will search a
text file of strings representing numbers of type int and will write the largest and the smallest numbers to the screen. The
file contains nothing but strings
Java read fileThere are many ways to
read a
file in
Java. DataInputStream class is used to
read text File line by line. BufferedReader is also used to
read a
file in
Java...
BufferedReader
BufferedReader class
read text from a character-input stream rather