java學習筆記(五)

來源:互聯網
上載者:User

標籤:

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class FileUtils {
/**

  • 列出指定檔案夾(目錄)中的所有檔案或目錄的名稱
  • @param dir File類型 指定的檔案夾(目錄)
  • @return
  • @throws IllegalAccessException
    /
    public static String listDirectory(File dir) throws IllegalAccessException{
    //判斷dir所關聯的檔案和目錄是否存在
    if(!dir.exists()){
    //如果不存在,那麼拋出異常
    throw new IllegalAccessException("目錄" + dir + "不存在。");
    }
    //判斷dir所關聯的是否是一個目錄
    if(!dir.isDirectory()){
    throw new IllegalAccessException(dir + "不是目錄");
    }
    /
    用傳遞進來 的File對象dir調用list()方法獲得
  • 目前的目錄(dir)下的所有檔案和檔案夾的名稱。
    */
    String[] files = dir.list();
    String m ="";
    for(String a : files){
    m=m+a+"\n";}
    return m;
    }
    /**
  • @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frame=new JFrame();
    JPanel main_panel =new JPanel(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String[] itme = {".gif",".bmp"};
JComboBox frm=new JComboBox(itme);
frm.setEnabled(true);
frm.setEditable(true);
frm.setMaximumRowCount(5);
frm.setBounds(230,30,130,25);

frame.setBounds(300,200,350,300);
frame.setVisible(true);
JTextArea main_text =new JTextArea();
main_text.setBackground(Color.white);
JScrollPane z=new JScrollPane();
z.setViewportView(main_text);
main_text.setEnabled(false);
main_panel.add(frm,BorderLayout.NORTH);
main_panel.add(z,BorderLayout.CENTER);
frame.add(main_panel);

try {
String str = FileUtils.listDirectory(new File("D:\FileUtils"));
main_text.setText(str);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

java學習筆記(五)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.