Results 1 to 9 of 9
- 07-14-2011, 09:31 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
Reading from a text file, then writing back to Text Area in Reverse
Hello,
I am working on a school assignment (GUI based) that will read an existing file consisting of single words on six different lines, then write these words in reverse order to a JTextArea. I am having trouble locating any guides to help me through the creation of an array and then reading from the array in reverse order. I have looked at creating a buffer and reading from it at specific points to get the characters but it seems there should be a better way with an array. Does anyone have any suggestions for sites to look at? The API pages are cryptic and give no examples. I have already created my GUI and have a fileinputstream working.
Thanks!
Jared
- 07-14-2011, 09:57 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What have you tried? Have you read the file into an array?(or the easier to work with ArrayList). Once you do that a simple loop can be used to build the string from the array or list.
- 07-14-2011, 10:01 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
re:
Actually, I am trying to work through this myself and I am looking for good documentation of array or arraylist with examples. Once I get a good grip on the concept I think I can work through it myself. I just can't find documentation with examples!
Thank you.
- 07-14-2011, 10:03 PM #4
Go to this site and Search for array:
The Really Big Index
- 07-14-2011, 10:11 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Here is a link to the tutorials on arrays: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
You can find a similar one for collections on the same site.
Edit: too slow :(
- 07-15-2011, 04:33 AM #6
This can be achieved with recursion but I guess you haven't covered that yet.
- 07-17-2011, 05:19 AM #7
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
Ok, Here's the code I have so far. Won't compile and I need a few pointers, please. I am getting errors about the textcopyarea and I need to now how to tweak the code to get my file "input.txt" as the input file Thanks!
Java Code:// Import Classes import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JCheckBox; import javax.swing.JTextArea; import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class FileToArray extends JFrame { public static void main(String[] args) { // Create Window JFrame frame = new JFrame(); frame.setSize(400, 250); frame.setTitle("File Reader"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create Text Area final JTextArea textcopyarea = new JTextArea(10, 20); textcopyarea.setText(""); // Create Button JButton button = new JButton("Process"); // Create Display Panel JPanel panel = new JPanel(); panel.add(button); panel.add(textcopyarea); frame.add(panel); // Listener Class class CalculateListener implements ActionListener { public void actionPerformed(ActionEvent event) { // Get Input From File try { String brline=""; String filebuffer[]=new String[100000]; int i=0; int j=0; RandomAccessFile file=new RandomAccessFile(filenametext.getText(),"r"); while((brline = file.readLine()) != null) { filebuffer[i]=brline; i++; } int len = i; file.close(); txtcopyarea.append("\r\n"); for(j = len; j>=0 ;j--) { txtcopyarea.append(filebuffer[j]); txtcopyarea.append("\r\n"); txtcopyarea.setLineWrap(true); txtcopyarea.setWrapStyleWord(false); txtcopyarea.setCaretPosition(txtcopyarea.getText() .length() - 1); } }catch(Exception e) { System.out.println(e); } } } ActionListener listener = new CalculateListener(); button.addActionListener(listener); frame.setVisible(true); } }Last edited by medic642; 07-17-2011 at 05:20 AM. Reason: spelling errors!
- 07-17-2011, 05:52 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
First thing I see is that you are importing nio classes, this can all be done with regular io(and probably should), nio is probably too advanced for you.
I would also suggest you not clump all code into the main method and instead have the main method only contain a line or two of code.
Why are you using random access file? Do you need to access into the file randomly? If not, use File.
All you need for io: http://download.oracle.com/javase/tu.../essential/io/
And for swing: http://download.oracle.com/javase/tu...ing/index.htmlLast edited by sunde887; 07-17-2011 at 06:13 AM.
- 07-17-2011, 02:38 PM #9
Similar Threads
-
Applet program to open a text file and display the content in text area
By bitse in forum Java AppletsReplies: 0Last Post: 12-09-2010, 05:56 PM -
How display text in listbox or text area right to left or center??
By sameer22 in forum CLDC and MIDPReplies: 0Last Post: 09-28-2010, 09:52 AM -
writing to text Area through Class
By dilpreet28 in forum New To JavaReplies: 3Last Post: 07-06-2010, 12:11 PM -
Reading and writing the contents of jtable into a text file
By Manfizy in forum NetBeansReplies: 6Last Post: 12-12-2008, 03:35 PM -
Reading and Writing Text Files
By kandt in forum New To JavaReplies: 1Last Post: 11-12-2008, 03:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks