我用的是jbuilder9
建了一个application 然后新建了一个 Frame,在里面添加了一JFileChooser,然后生成对象
JFileChooser1
然后想打开一个txt文档,不知道怎么做了,请各位帮忙实现一下。

解决方案 »

  1.   

    JFileChooser fileChooser = new JFileChooser();
        fileChooser.addChoosableFileFilter(new yourfilter());  //自己写个filter类
      

  2.   

    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.filechooser.*;public class FileChooserDemo extends JFrame {
        static private final String newline = "\n";    public FileChooserDemo() {
            super("FileChooserDemo");        //Create the log first, because the action listeners
            //need to refer to it.
            final JTextArea log = new JTextArea(5,20);
            log.setMargin(new Insets(5,5,5,5));
            log.setEditable(false);
            JScrollPane logScrollPane = new JScrollPane(log);        //Create a file chooser
            final JFileChooser fc = new JFileChooser();        //Create the open button
            ImageIcon openIcon = new ImageIcon("images/open.gif");
            JButton openButton = new JButton("Open a File...", openIcon);
            openButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int returnVal = fc.showOpenDialog(FileChooserDemo.this);                if (returnVal == JFileChooser.APPROVE_OPTION) {
                        File file = fc.getSelectedFile();
                        //this is where a real application would open the file.
                        log.append("Opening: " + file.getName() + "." + newline);
                    } else {
                        log.append("Open command cancelled by user." + newline);
                    }
                }
            });        //Create the save button
            ImageIcon saveIcon = new ImageIcon("images/save.gif");
            JButton saveButton = new JButton("Save a File...", saveIcon);
            saveButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    int returnVal = fc.showSaveDialog(FileChooserDemo.this);                if (returnVal == JFileChooser.APPROVE_OPTION) {
                        File file = fc.getSelectedFile();
                        //this is where a real application would save the file.
                        log.append("Saving: " + file.getName() + "." + newline);
                    } else {
                        log.append("Save command cancelled by user." + newline);
                    }
                }
            });        //For layout purposes, put the buttons in a separate panel
            JPanel buttonPanel = new JPanel();
            buttonPanel.add(openButton);
            buttonPanel.add(saveButton);        //Explicitly set the focus sequence.
            openButton.setNextFocusableComponent(saveButton);
            saveButton.setNextFocusableComponent(openButton);        //Add the buttons and the log to the frame
            Container contentPane = getContentPane();
            contentPane.add(buttonPanel, BorderLayout.NORTH);
            contentPane.add(logScrollPane, BorderLayout.CENTER);
        }    public static void main(String[] args) {
            JFrame frame = new FileChooserDemo();        frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });        frame.pack();
            frame.setVisible(true);
        }
    }
      

  3.   

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    class opendisk extends JFrame implements ActionListener
    {
    opendisk()
    {
    super("open disk");
    JPanel pane=new JPanel();
    JButton open=new JButton("open");
    open.addActionListener(this);
    open.setActionCommand("open");
    pane.add(open);
    setContentPane(pane);
    show();
    }
    public static void main(String argu[])
    {
    opendisk od=new opendisk();
    od.pack();
    }
    public void actionPerformed(ActionEvent evt)
    {
    String cmd=evt.getActionCommand();
    if(cmd=="open")
    {
    JFileChooser JFileChooser1 = new JFileChooser("D:\\");//启动一个文件选择器
    if(JFileChooser1.APPROVE_OPTION==JFileChooser1.showOpenDialog(this))//如果文件选择完毕
    {
    openFile(JFileChooser1.getSelectedFile().getPath());//作为将来的接口
    String filepath=JFileChooser1.getSelectedFile().getPath();//获取被选择文件的路径
    System.out.println(filepath);//输出文件路径
    }
    }
    }
    void openFile(String fileName) 
        {
    try 
    {
    File file=new File(fileName);
    int size =(int)file.length();
    int chars_read=0;
    FileReader in=new FileReader(file);
    char[] data=new char[size];
    while(in.ready())
    {
    chars_read+=in.read(data,chars_read,size-chars_read);
    //read(目标数组、文件起始位置、文件结束位置)
    //返回读入的数据量
    }
    in.close();
    }
    catch(IOException e)
    {
    System.out.println(e.toString());
      }
    }}
      

  4.   

    象这种初学者经常会遇到的基础问题,建议搂住先用csdn的搜索和全文检索功能搜一下,答案马上就能找到,找不到的时候再发贴子问。