简体   繁体   中英

How do i transfer the text in Jtextarea to a text file using printwriter?

How can I transfer texts to a text file from a text area to a text file using Printwriter?

Where do i put the code: Printwriter f=new Printwriter("") ? And If a method has a function: System.out.println , how do I place what the method will print to the Jtextarea?

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    public class ReliefFrame extends JFrame
    {
    ......

    }

    private void createNorthPanel()
    {
        .....
    }
    private void createEastPanel()
    {
        .....
    }
    private void createWestPanel()
    {
        .....
    }
    private void createSouthPanel()
    {
        ......
    }

    private void createaTextField()
    {
        .....
    }
    private void createTextArea()
    {
        ......
    }   
    private void createCButton()
    {
        button[2] = new JButton( "Release Packs" );
        class ButtonListener implements ActionListener
        { 
            public void actionPerformed( ActionEvent ae )
            {
                String a = bTextField.getText();
                int b=Integer.parseInt(a);
                s.releasePacks(b);
                bTextField.setText( "" );           
            }
        }
        button[2].addActionListener( new ButtonListener() );
        rowwww.add( button[2] ); 
    }
    private void createEButton()
    {
        button[4] = new JButton( "Inventory Report" );
        class ButtonListener implements ActionListener
        { 
            public void actionPerformed( ActionEvent ae )
            {

                aTextArea.append("===INVENTORY===\n");
                s.printInventory();
                aTextArea.append("===============\n");

            }
        }
        button[4].addActionListener( new ButtonListener() );
        rowwww.add( button[4] ); 
    }
    public static void main(String[] arguments) {
    ReliefFrame c = new ReliefFrame();
    }

  }

There's a lot of ways this sort of thing can be done but you need to decide where you want to place the code by figuring out what you want the User to do to in order to fire it. Could be the selection of a JButton or perhaps a menu selection. That is something you will need to decide. Let's say a JButton since you've created some already.

Make another button and enter the word "Save" for the button text then make sure it too has the listener with the actionPerformed event like you did with the other buttons then place the code below into it. It should look something like:

public void actionPerformed( ActionEvent evt )
    try {
        // Be sure to supply the path and file name you want.
        PrintWriter printwriter = new PrintWriter("c:\\temp\\MyTextFile.txt");

        String[] lines = yourJTextArea.getText().split("\\n");
        for(int i = 0 ; i < lines.length; i++) {
            printwriter.println(lines[i]);
        }
        printwriter.close();
    } 
    catch (IOException e) {
        //Do something with caught exception.
    }
}

Now, question below I don't quite understand. You will need to explain that with much more explicit detail but what I think you're trying to say is:

Your Question:

And If a method has a function:System.out.println, how do I place what the method will print to the Jtextarea?

What I think it means:

"Hey, I got my hands on a method I want to use from the net but I can't for the life of me figure out how to get the result from this method into my JTextArea control. Right now the method spits out the method result to the output console (or pane)."

If this is indeed the case then you can replace the System.out.println(); method with:

yourJTextArea.append(that darn method result string);

I hope this has helped you somewhat.

Use the write(...) method provided by the JTextArea API:

FileWriter writer = new FileWriter( ... );
BufferedWriter bw = new BufferedWriter( writer );
textArea.write( bw );
bw.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM