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

 
 


Vicki Schmidt
Java Read Lines from Text File and Output in Reverse order to a Different Text File
0 Answer(s)      9 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;

}

View Answers









Related Pages:
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
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
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
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
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
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
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
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
Redirecting the console output into a file in Java
Redirecting the console output into a file in Java  Hi, i coded a program that read specific lines from a text file. It's working well. Now i want to redirect the output of the console to a text file. i used your tutorial
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
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
Count lines of a particular file
C:\vinod\Math_package>javac NumberOfLine.java C:\vinod\Math_package>java NumberOfLine Getting line number of a paritcular file example! Please enter file name with extension: AddTwoBigNumbers.shtml
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
JavaScript write to text file
a TextStream object to read from or write to the file. The Boolean value defined... JavaScript write to text file  ... are going to create a file and write text into it using JavaScript. In the given example
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
Java file number of lines
Java file number of lines In this section, you will learn how to count the number of lines from the given file. Description of code: Java has provide... the lines from the file. You can see in the given example, we have created
Reading text from image file - Java Beginners
Reading text from image file  How Read text from image file
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
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
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
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
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
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
Text File I/O - DVD.java - Java Beginners
Text File I/O - DVD.java  NEED HELP PLEASE. The application should use a text file with one value on each line of the file. Each movie would use... starts, read the data file and populate the array. When the file ends, use
how to write and read text for javaME
from the library? i want to type multiple line on text file then, read it from java me coding.. can u helps me? it is urgent. i have to submit this project...how to write and read text for javaME  Hi. I have tried ur read
Write Text into File
to write text into file. At last close output file using close() method... Write Text into File      ... "write.txt". We are using FileWriter class to read file
Java Read File Line by Line - Java Tutorial
Java Read File Line by Line - Java Tutorial... Tutorial you will learn how to write java program to read file line by line. We will use the DataInputStream class to Read text File Line by Line. Class
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
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
how to update the text file?
how to update the text file?  my textfile with name list.txt: Rice... 150 .... ....20 lines. java code: import java.io.*; import java.util.*; class... in my text file, if the item i entered matches with item in text file.how can i
Read Text from Standard IO
facilities for reading text from either the file or the keyboard on the command... Reading Text from the Standard Input   ... from the keyboard and write output to the display. They also support I/O
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
Reverse Order Java
Reverse Order Java   I am trying to get this into reverse order. Does... in reverse order: "); for (int i = numbers.length-1; i >=0; i...); System.out.println("Numbers in reverse order: "); for (int i = numbers.length-1; i
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
Java file line reader
Java file line reader In this section, you will learn how to read a particular line from file. Sometimes you want to read a particular line from the file but you have to read the whole file for this process. So in order to remove
display repeated lines in a file
display repeated lines in a file  i want a java program that accepts file from user and displays the repeated lines in a file
Read File in Java
Read File in Java This tutorial shows you how to read file in Java. Example.... BufferedReader The class java.io.BufferedReader is used to read the text from... shows you how to read file in Java * */ public class ReadFileExample { public
input output
by the ObjectStreamException during a write operating. Read Text from Standard IO: Java... stream. BufferedReader This class provides read text from... provides write text from character output stream and buffering characters
Display non-duplicate words from file
Display non-duplicate words from file In this tutorial, you will learn how to read a text file and display non-duplicate words in ascending order. The given code reads the file and stored the file text into StringBuffer. The buffer
How to format text file? - Java Beginners
How to format text file?  I want the answer for following Query ***(Java code) How to open,read and format text file(notepad) or MS-word.../example/java/io/java-read-file-line-by-line.shtml Thanks
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
JavaScript reverse text string
JavaScript reverse text string...; In this section, we are going to reverse the text string using JavaScript..., you will get the reverse string in the same text box. Here is the code
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
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

Ask Questions?

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.