Java Read Lines from Text File and Output in Reverse order to a Different Text File

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:
Java Read Lines from Text File and Output in Reverse order to a Different Text File
Java Read Lines from Text File and Output in Reverse order to a Different Text... by line and output in reverse order to the text area field. Can someone help... the JButton. Each line in the file then needs to be converted in Reverse Order
Read Lines from text file
Read 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 file
Java 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
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and filewriter
To dump 10lakh lines from a text file to a mysql table
To dump 10lakh lines from a text file to a mysql table  I need to dump a text file datas -100000 lines into a mysql table.i'm able to dump only 152000 lines only
Read specific column data from text file in java
Read specific column data from text file in java  My question is if my text file contain 15 columns and i want read specific column data from that text file then what code i should do
Read Text file from Javascript - JSP-Servlet
Read Text file from Javascript  plz send the code How to Retrieve the data from .txt file thru Javascript? And how to find the perticular words in that file
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
Search and return lines of text file
Search 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
Program to read the text from a file and display it on a JFrame.
Program to read the text from a file and display it on a JFrame.  import javax.swing.*; import java.io.*; import java.lang.*; import java.awt.*; class MegaViewer1 extends JFrame { JTabbedPane jtp1=new JTabbedPane
Read text File
Read text File  Hi,How can I get line and keep in a String in Java
in unix which command is used to sort the lines of data in a file in reverse order
the lines of data in a file in reverse order...in unix which command is used to sort the lines of data in a file in reverse order  in unix which command is used to sort the lines of data in a file
Steps to read text file in pyspark
Steps 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 java
how 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
Reading text from image file - Java Beginners
Reading text from image file  How Read text from image file
Java program to read a text file and write to another file
Java program to read a text file and write to another file - Creating... and we want to copy the content into another text file from our Java program.... Our requirement is to read a text file and then write the content of the text
How to read and compare content of two different text file
Description: In the given example you will see how a two text file's content are compared. The BufferedReader class allow us to read a file. The readLine() method used to read the contents of the specified file.  The way
Character from text file - Java Beginners
Character 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 Java
Delete 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 PySpark
Read 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
How to read the data in text file seperated by by ',' in java using IO Operations
How to read the data in text file seperated by by ',' in java using IO Operations  in Text file data like raju 45,56,67 ramu 46,65,78 raji 34,23,56 this is the student marks in text file.this data read and calculate
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 java
Read 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 api
how 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 Java
How 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 Java
Read 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 - JDBC
read 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.0
how 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
inserting text into text file using java application
inserting text into text file using java application  Hi, I want to insert a text or string into a text file using java application
convert data from pdf to text file - Java Beginners
convert data from pdf to text file   how to read the data from pdf file and put it into text file(.txt
stop word removal from text file
stop word removal from text file  i need java source code for stop word removal from a text file
text file
text 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
Create text file at client's directory from server.
Create text file at client's directory from server.  Need java code to create text file at client's directory from server..... Please Help
Java search word from text file
Java 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 file
binary 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
Importing data into sql plus from a text file...
Importing data into sql plus from a text file...  How to import a text file into oracle 10g enterprise edition directly to create tables and data
Importing data into sql plus from a text file...
Importing data into sql plus from a text file...  How to import a text file into oracle 10g enterprise edition directly to create tables and data
save text file - Java Beginners
save 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 Java
Count 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 Java
How 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 file
how 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
How to make a list from file text in J2ME - Java Beginners
How to make a list from file text in J2ME  I was trying to make a method that read file from text and make a list of it, I have tried ReadHelpText... is return: icon ide so I mean, it read the kamus.txt file contents
Adding a text file - Java Beginners
Adding 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 file
There 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

Ads