Solved

Saving edited text to a file from a JTextArea

Posted on 2009-05-12
7
1,425 Views
Last Modified: 2013-11-23
I keep trying to compile the following code below however after i attempted to implement a way to save the modified text from the JTextArea i keep getting the following error during compile:

fileio.java:158: cannot find symbol
symbol  : constructor FileWriter(java.io.File)
location: class FileWriter
         FileWriter first = new FileWriter(new File("first.txt"));

I loaded:
import java.io.*;


So i am very confused as to why I am getting this error. Any help would be appreciated.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.*;
 
 
 
 
 
public class fileio
{
	String word;
	JTextArea wordDocModify;
	String openedDoc;
	public static void main (String[] args)throws Exception
	{
	fileio docEdit = new fileio();
	docEdit.textEditorInitialScreen();
//	docEdit.docEditor();
 
	}
 
public void textEditorInitialScreen() throws Exception
{
	
  	  JFrame jF1 = new JFrame("Text Editor"); // Set the window title.
 
      // Set the size of the JFrame to 1150 x 629 pixels. 
      jF1.setSize(1150, 629);
		
		
		JLayeredPane jP1 = new JLayeredPane();
		jP1.setLayout(null);
		
			
		JTextArea wordDocNew = new JTextArea(5, 10);
		wordDocNew.setLineWrap(true);
		
		
				JButton startOpen = new JButton("OPEN");
		           startOpen.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e)
						{
					//	textEditorEditingScreen();
					try {
    textEditorEditingScreen();
						} 
catch (IOException e1){
    e1.printStackTrace();
}
						}
            });
		
		JButton startSave = new JButton("SAVE");
  			         startSave.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) 
						{
						 System.exit(0);
						}
            });
 
		JButton startClose = new JButton("QUIT");
		           startClose.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) 
						{
						 System.exit(0);
						}
            });
				
	
		
      // Post the JLabels on the JPanel
	   // jPanel1.add(pictureJLabel); // Post the BSG start screen. 
		jP1.add(wordDocNew, JLayeredPane.PALETTE_LAYER);
		jP1.add(startClose, JLayeredPane.PALETTE_LAYER); 
		jP1.add(startOpen, JLayeredPane.PALETTE_LAYER);
		jP1.add(startSave, JLayeredPane.PALETTE_LAYER);
		
		wordDocNew.setBounds(10,20,900,500);
		startOpen.setBounds(970,50,100,20);
		startSave.setBounds(970,100,100,20);
		startClose.setBounds(970,200,100,20);
 
 
 
      // Post the JPanel into the JFrame
      jF1.getContentPane().add(jP1);
		
      // Displays the JFrame
      jF1.setVisible(true);
		
 
}
 
public void textEditorEditingScreen() throws IOException
{
 
	 //FileReader dataIn = new FileReader("text.txt");
	 String filename = File.separator + "tmp";
    JFileChooser fc = new JFileChooser(new File(filename));
    // Show open dialog
    fc.showOpenDialog(null);
    File f = fc.getSelectedFile();
 
    // Show save dialog
 //   fc.showSaveDialog(null);
 //   selFile = fc.getSelectedFile();
	 //   File f = new File("first.txt");
    FileReader fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);
 
    StringBuffer sb = new StringBuffer();
    String eachLine = br.readLine();
    while (eachLine != null) {
      sb.append(eachLine);
      sb.append("\n");
      eachLine = br.readLine();
    }
 
	 
	
  	  JFrame jF2 = new JFrame("Text Editor"); // Set the window title.
 
      // Set the size of the JFrame to 1150 x 629 pixels. 
      jF2.setSize(1150, 629);
		
		
		JLayeredPane jP2 = new JLayeredPane();
		jP2.setLayout(null);
		
		
	 wordDocModify = new JTextArea(sb.toString(), 5, 10);
		wordDocModify.setLineWrap(true);
		
 
				JButton modifyOpen = new JButton("OPEN");
		           modifyOpen.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) 
						{
 
						}
            });
		
		JButton modifySave = new JButton("SAVE");
  			         modifySave.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) 
						{
						word = wordDocModify.getText();
	 			    try{
    // Create file 
	 
  	 FileWriter first = new FileWriter(new File("first.txt"));
    first.write("First file in java");
	//Close the output stream
	first.close();  
    
 
    }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
	 		//			FileWriter first = new FileWriter(new File("test.txt"));
			/*	FileWriter fstream = new FileWriter("out.txt");
     			 BufferedWriter out = new BufferedWriter(fstream);
 					   out.write(word);
    //Close the output stream
    out.close(); */
 
						System.out.println("written the following " + word + " to a file");
						}
            });
 
		JButton modifyClose = new JButton("QUIT");
		           modifyClose.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) 
						{
						 System.exit(0);
						}
            });
				
	
		
      // Post the JLabels on the JPanel
	   // jPanel1.add(pictureJLabel); // Post the BSG start screen. 
		jP2.add(wordDocModify, JLayeredPane.PALETTE_LAYER);
		jP2.add(modifyClose, JLayeredPane.PALETTE_LAYER); 
		jP2.add(modifyOpen, JLayeredPane.PALETTE_LAYER);
		jP2.add(modifySave, JLayeredPane.PALETTE_LAYER);
		
		wordDocModify.setBounds(10,20,900,500);
	//	modifyOpen.setBounds(970,50,100,20);
		modifySave.setBounds(970,100,100,20);
		modifyClose.setBounds(970,200,100,20);
 
 
 
      // Post the JPanel into the JFrame
      jF2.getContentPane().add(jP2);
		
      // Displays the JFrame
      jF2.setVisible(true);
}
 
public String test()
{
	
			String word = wordDocModify.getText();
		System.out.println("hey" + word + "test");
		return word;
		
}
 
}

Open in new window

0
Comment
Question by:derricklu
  • 4
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24369555
If you use a JEditorPane instead, loading and saving become much easier:
Document doc = ep.getDocument();
ep.getEditorKit().write(fileWriter, doc, 0, doc.getLength());

Open in new window

0
 

Accepted Solution

by:
derricklu earned 0 total points
ID: 24369573
I wanted to do it my way >-<
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24369613
Try
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
 
import java.io.*;
 
import javax.swing.*;
import javax.swing.text.*;
 
 
public class fileio {
    String word;
    JTextArea wordDocModify;
    String openedDoc;
 
    public static void main(String[] args) throws Exception {
        fileio docEdit = new fileio();
        docEdit.textEditorInitialScreen();
 
        //      docEdit.docEditor();
    }
 
    public void textEditorInitialScreen() throws Exception {
        JFrame jF1 = new JFrame("Text Editor"); // Set the window title.
 
        // Set the size of the JFrame to 1150 x 629 pixels. 
        jF1.setSize(1150, 629);
 
        JLayeredPane jP1 = new JLayeredPane();
        jP1.setLayout(null);
 
        JTextArea wordDocNew = new JTextArea(5, 10);
        wordDocNew.setLineWrap(true);
 
        JButton startOpen = new JButton("OPEN");
        startOpen.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //      textEditorEditingScreen();
                    try {
                        textEditorEditingScreen();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });
 
        JButton startSave = new JButton("SAVE");
        startSave.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
 
        JButton startClose = new JButton("QUIT");
        startClose.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
 
        // Post the JLabels on the JPanel
        // jPanel1.add(pictureJLabel); // Post the BSG start screen. 
        jP1.add(wordDocNew, JLayeredPane.PALETTE_LAYER);
        jP1.add(startClose, JLayeredPane.PALETTE_LAYER);
        jP1.add(startOpen, JLayeredPane.PALETTE_LAYER);
        jP1.add(startSave, JLayeredPane.PALETTE_LAYER);
 
        wordDocNew.setBounds(10, 20, 900, 500);
        startOpen.setBounds(970, 50, 100, 20);
        startSave.setBounds(970, 100, 100, 20);
        startClose.setBounds(970, 200, 100, 20);
 
        // Post the JPanel into the JFrame
        jF1.getContentPane().add(jP1);
 
        // Displays the JFrame
        jF1.setVisible(true);
    }
 
    public void textEditorEditingScreen() throws IOException {
        //FileReader dataIn = new FileReader("text.txt");
        String filename = File.separator + "tmp";
        JFileChooser fc = new JFileChooser(new File(filename));
        // Show open dialog
        fc.showOpenDialog(null);
 
        File f = fc.getSelectedFile();
 
        // Show save dialog
        //   fc.showSaveDialog(null);
        //   selFile = fc.getSelectedFile();
        //   File f = new File("first.txt");
        FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);
 
        StringBuffer sb = new StringBuffer();
        String eachLine = br.readLine();
 
        while (eachLine != null) {
            sb.append(eachLine);
            sb.append("\n");
            eachLine = br.readLine();
        }
 
        JFrame jF2 = new JFrame("Text Editor"); // Set the window title.
 
        // Set the size of the JFrame to 1150 x 629 pixels. 
        jF2.setSize(1150, 629);
 
        JLayeredPane jP2 = new JLayeredPane();
        jP2.setLayout(null);
 
        wordDocModify = new JTextArea(sb.toString(), 5, 10);
        wordDocModify.setLineWrap(true);
 
        JButton modifyOpen = new JButton("OPEN");
        modifyOpen.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                }
            });
 
        JButton modifySave = new JButton("SAVE");
        modifySave.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    word = wordDocModify.getText();
 
                    try {
                        // Create file 
                        FileWriter first = new FileWriter(new File("first.txt"));
                        first.write("First file in java");
                        //Close the output stream
                        first.close();
                    } catch (Exception ex) { //Catch exception if any
                        System.err.println("Error: " + ex.getMessage());
                    }
 
                    //                      FileWriter first = new FileWriter(new File("test.txt"));
                    /*      FileWriter fstream = new FileWriter("out.txt");
                            BufferedWriter out = new BufferedWriter(fstream);
                            out.write(word);
                    //Close the output stream
                    out.close(); */
                    System.out.println("written the following " + word +
                        " to a file");
                }
            });
 
        JButton modifyClose = new JButton("QUIT");
        modifyClose.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
 
        // Post the JLabels on the JPanel
        // jPanel1.add(pictureJLabel); // Post the BSG start screen. 
        jP2.add(wordDocModify, JLayeredPane.PALETTE_LAYER);
        jP2.add(modifyClose, JLayeredPane.PALETTE_LAYER);
        jP2.add(modifyOpen, JLayeredPane.PALETTE_LAYER);
        jP2.add(modifySave, JLayeredPane.PALETTE_LAYER);
 
        wordDocModify.setBounds(10, 20, 900, 500);
        //      modifyOpen.setBounds(970,50,100,20);
        modifySave.setBounds(970, 100, 100, 20);
        modifyClose.setBounds(970, 200, 100, 20);
 
        // Post the JPanel into the JFrame
        jF2.getContentPane().add(jP2);
 
        // Displays the JFrame
        jF2.setVisible(true);
    }
 
    public String test() {
        String word = wordDocModify.getText();
        System.out.println("hey" + word + "test");
 
        return word;
    }
}

Open in new window

0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:derricklu
ID: 24369643
What did you do?!!?!?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24369674
You had trouble around exception handling in the save action
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24369683
(the variable name of the exception clashed with your event variable name)
0
 
LVL 92

Expert Comment

by:objects
ID: 24369961
fileio.java:158: cannot find symbol
symbol  : constructor FileWriter(java.io.File)
location: class FileWriter
         FileWriter first = new FileWriter(new File("first.txt"));


I don't that error when compiling your code
Your code looks fine in that respect


The problem CEHJ has changed is that the ActionEvent and the Exception var had the same name, though that would ne cause the error you were getting

                  public void actionPerformed(ActionEvent e)
....
    }catch (Exception e){//Catch exception if any


0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 51
jsp insert to database example 2 47
add projects t working set in maven 2 23
passing enum to a method 4 23
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question