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

Ads
 

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

Ads









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
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
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
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
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
. Here is the video insturction "How to read text file in Java?"... a text file one line at a time. It can also be used to read large text files... by other program. Here we have used DataInputStream class to read the text 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... and the class is used to read data from a file. The could be text file
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
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
Read text File
Read text File  Hi,How can I get line and keep in a String in 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
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
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
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
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
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
Read text file in PySpark
Read text file in PySpark - How to read a text file in PySpark? The PySpark... configuration conf = SparkConf().setAppName("read text file in pyspark") sc... context with Spark configuration conf = SparkConf().setAppName("read 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
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
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
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
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
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
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
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
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 class read text from a character-input stream rather than read one

Ads