1. 程式人生 > >JAVA讀取資料夾裡的檔案目錄到TXT

JAVA讀取資料夾裡的檔案目錄到TXT

package javapackge;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.JFrame;

class MyWindowDemo{
    private Frame f;
    private TextField tf;
    private Button btn;
    private TextArea ta;
    MyWindowDemo()
    {
        init();
    }
    public void init(){
        f
=new JFrame("my window"); f.setBounds(300, 100, 600, 500); f.setLayout(new FlowLayout()); tf=new TextField(30); btn=new Button("轉到"); ta=new TextArea(15,40); f.add(tf); f.add(btn); f.add(ta); myEvent(); f.setVisible(true); }
private void myEvent(){ btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String dirpath=tf.getText(); File dir=new File(dirpath); if(dir.exists() && dir.isDirectory()){ ta.setText(
""); String[]names=dir.list(); for(String name : names){ ta.append(name+"\r\n"); } } } }); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }); } } public class Dumulu { public static void main(String[] args) { new MyWindowDemo(); } }