Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Java Read Lines from Text File and Output in Reverse order to a Different Text File
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Vicki Schmidt
Java Read Lines from Text File and Output in Reverse order to a Different Text File
0 Answer(s)      2 years and 3 months ago
Posted in : Java Beginners


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;

}


Advertisement
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
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
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
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 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
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
Read text File
Read text File  Hi,How can I get line and keep in a String in Java
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
Java read text file
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.... Example of Read text File Line by Line: import java.io.*; class FileRead
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
Reading text from image file - Java Beginners
Reading text from image file  How Read text from image file
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
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
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
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
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
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
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
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
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
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
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
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
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
 

 

 

DMCA.com