7 Replies - 16765 Views - Last Post: 29 May 2011 - 10:50 AM Rate Topic: -----

#1 realTimeStrategy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-February 11

Writing to a file from a GUI text field and then reading it

Posted 28 May 2011 - 04:45 PM

This is a GUI program containing a text field, a button, and a text area where the user enters a String in the text field box, and when the button is clicked, the program writes that String into a file. Next, the program has to read from that file and paste it in the text area box.

This is a very simple program; I don't know why I can't figure this out. Every time I try to read from the file, I receive a "Unhandled Exception Type" error. The writing to the file works, but the reading doesn't.

Here is the code:

import java.awt.*; 
import java.awt.event.*;
import java.io.*;
 
public class Project9 extends Frame
{ 
	TextField textfield1; 
	Button button;
	TextArea textarea = new TextArea("",20,20,20);
	
	File outFile = new File("I://TextField.txt");;
	FileOutputStream outFileStream = new FileOutputStream(outFile);
	PrintWriter outStream = new PrintWriter(outFileStream);
	
	File inFile = new File("I://TextField.txt");
	FileReader myFileReader = new FileReader(inFile);
	BufferedReader myBufReader = new BufferedReader(myFileReader);

	public Project9() throws IOException
	{
		setTitle("Project9");
		setSize(400,400); 
		setLayout(new FlowLayout()); 
 
		textfield1 = new TextField("",10);
		button = new Button("Click");
	  
		add(textfield1);
		add(button); 
		add(textarea);
		
		addWindowListener(new WindowAdapter() 
		{ 
			public void windowClosing(WindowEvent e) 
			{ 
				System.exit(0); 
			} 
		});
		
		button.addActionListener(new ActionListener() 
		{ 
			public void actionPerformed(ActionEvent e)
			{	
		        outStream.write(textfield1.getText());
                        //textarea.append(myBufReader.readLine());
                        //myBufReader.close();
                        outStream.close();
			} 
		});
	}

	public static void main(String[] args) throws IOException 
	{ 
		Project9 guiInterface = new Project9(); 
		guiInterface.setVisible(true);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Writing to a file from a GUI text field and then reading it

#2 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Writing to a file from a GUI text field and then reading it

Posted 28 May 2011 - 04:54 PM

Could you copy/paste the details from the exception? That will help us help you better.
Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Writing to a file from a GUI text field and then reading it

Posted 28 May 2011 - 04:56 PM

Firstly, I wouldnt place the reading and writing of the file in the class scope, I would place it in a method to slightly seperate things. You can do something like this
        File file = new File("I://TextField.txt");
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                contents.append(text)
                    .append(System.getProperty(
                        "line.separator"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


The error you were receiving basically tells you that you are not handling any exceptions which can occur with this. Placing it in a try and catch block will solve this issue.
Was This Post Helpful? 2
  • +
  • -

#4 realTimeStrategy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-February 11

Re: Writing to a file from a GUI text field and then reading it

Posted 28 May 2011 - 04:58 PM

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type IOException

at Project9.<init>(Project9.java:46)
at Project9.main(Project9.java:54)
Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Writing to a file from a GUI text field and then reading it

Posted 28 May 2011 - 05:05 PM

With the solution I gave you, you can write to your Text area by
textarea.append(contents.toString());


Once again though, if you continue to get the error, its because you will need to use try and catch block within your ActionEvent aswell.
Was This Post Helpful? 0
  • +
  • -

#6 realTimeStrategy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-February 11

Re: Writing to a file from a GUI text field and then reading it

Posted 28 May 2011 - 05:08 PM

Thank you for your advice, nick2price, I will play around with this and see what happens.
Was This Post Helpful? 0
  • +
  • -

#7 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Writing to a file from a GUI text field and then reading it

Posted 28 May 2011 - 05:17 PM

Kool. Let me tell you why you should try and create a method for this. At the top of your code, your going to be placing everything in a try and catch block. You are then going to be repeating this in the ActionEvent. If you have one method which reads a file, and one which writes, its as simple as this. In the action event, grab the text from a text field and assign it to a String. Pass this String to the write file method. And thats it. The method will handle all the writing etc.
I am off to bed now so if you have any problems, post back, and someone else will give you a hand.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,770
  • Joined: 27-December 08

Re: Writing to a file from a GUI text field and then reading it

Posted 29 May 2011 - 10:50 AM

Also, you should consider using the more modern Swing Components like JFrame, JTextArea, JButton, etc., over the antiquated AWT Components like Frame, TextArea, Button, etc.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1