如何使用jfilechooser在java中保存文件?

时间:2014-04-30 07:37:45

标签: java swing jfilechooser

在这段代码中我可以打开文本文件并在Windows面板上显示它..但是去的时候     保存它会保存...但保存的文件无法在保存的目录中找到,请帮助我...我是java新手...提前致谢

import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Read extends JFrame
{
    private JTextField  filename    = new JTextField(), dir = new JTextField();
    private JButton     open        = new JButton( "Open" ), save = new JButton( "Save" );
    public JPanel       window_panel;                                                       // address_panel,
    public JLabel       address_label;
    public JTextField   address_tf;
    public JEditorPane  window_pane, tree_pane, attr_pane;
    public JScrollPane  window_scroll, tree_scroll, attr_scroll;
    TextArea            t1, t2;
    JPanel              pane;
    public JButton      address_b, browse;
    public JLabel       l, m;
    JFrame              f;

    public Read() throws IOException
    {

        f = new JFrame( "Web browser" );
        f.setSize( 1000, 700 );

        pane = new JPanel();
        pane.setVisible( true );
        pane.setLayout( null );
        f.setContentPane( pane );

        address_label = new JLabel( " Path: ", SwingConstants.CENTER );
        address_label.setBounds( 10, 10, 70, 30 );
        pane.add( address_label );

        address_tf = new JTextField( "", 25 );
        address_tf.setBounds( 80, 10, 250, 30 );
        pane.add( address_tf );

        browse = new JButton( "Browse" );
        browse.setBounds( 340, 10, 140, 30 );
        browse.addActionListener( new Open() );
        pane.add( browse );

        save = new JButton( "Save" );
        save.setBounds( 500, 10, 150, 30 );
        save.addActionListener( new Save() );
        pane.add( save );

        window_pane = new JEditorPane();
        window_pane.setBounds( 10, 50, 970, 600 );
        pane.add( window_pane );

        f.setVisible( true );
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    }

    class Open implements ActionListener
    {
        public void actionPerformed( ActionEvent ae )
        {
            JFileChooser fc = new JFileChooser();

            int result = fc.showOpenDialog( null );
            if ( result == JFileChooser.APPROVE_OPTION )
            {
                File file = fc.getSelectedFile();
                String sname = file.getAbsolutePath();
                address_tf.setText( sname );
                String ext = getFileExtension( sname );
                try
                {

                    window_pane.setPage( file.toURI().toURL() );
                    System.out.println( file.toURI().toURL() );
                }
                catch ( MalformedURLException e )
                {
                    e.printStackTrace();

                }
                catch ( IOException e )
                {
                    e.printStackTrace();

                }
            }
        }
    }

    class Save implements ActionListener
    {
        public void actionPerformed( ActionEvent e )
        {
            JFileChooser sv = new JFileChooser();
            // Demonstrate "Save" dialog:
            int result = sv.showSaveDialog( Read.this );
            if ( result == JFileChooser.APPROVE_OPTION )
            {
                filename.setText( sv.getSelectedFile().getName() );
                dir.setText( sv.getCurrentDirectory().toString() );
            }

        }
    }

    public String getFileExtension( String filename )
    {
        if ( filename == null )
        {
            return null;
        }
        int lastUnixPos = filename.lastIndexOf( '/' );
        int lastWindowsPos = filename.lastIndexOf( '\\' );
        int indexOfLastSeparator = Math.max( lastUnixPos, lastWindowsPos );
        int extensionPos = filename.lastIndexOf( '.' );
        int lastSeparator = indexOfLastSeparator;
        int indexOfExtension = lastSeparator > extensionPos ? -1 : extensionPos;
        int index = indexOfExtension;
        if ( index == -1 )
        {
            return "";
        }
        else
        {
            return filename.substring( index + 1 );
        }
    }

    public static void main( String args[] ) throws IOException
    {

        Read wb = new Read();

    }
}

2 个答案:

答案 0 :(得分:0)

actionPerformed方法中,您实际上保存文件。您只需将其名称和文件夹存储到变量中。

答案 1 :(得分:0)

使用JfileChooser可以获得路径。然后,您可以将您想要的内容复制到该位置。

这是示例代码

   jFileChooser1.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);

    jFileChooser1.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);

    final int fd = jFileChooser1.showDialog(null, "Select folder to save.");

    if (fd == JFileChooser.APPROVE_OPTION) {

        /*you can write the code in here */

        String setBackUpFolderName = setBackUpFolderName(iivo);
        try {
            saveDataToFileExcel(setBackUpFolderName, iivo);
        } catch (Exception ex) {
            Logger.getLogger(ExcelTest.class.getName()).log(Level.SEVERE, null, ex);
        }

    } else {
        // this.dispose();
    }