java实现CDEF盘的读取

今天用java写了一个简单的文件的读取,如果是文件夹则会只显示当前文件夹下的文件,如果是文件则会显示文件的内容,不过写的比较粗糙,只有对。txt的文件进行内容的显示.

用到swingworker来实现文件的读取。

本来想写的复杂一点,但是由于在jlist相应事件中不能同时对它自己进行很多的操作,最后尝试了半天只好改写成先在jlist中选中一个文件,然后使用按钮吧。这样解决了。

java界面,还有线程用的不好。今后还得加强练习。

下面就是代码。里面的图片可以改成自己的图片也可以,不过要放在与当前的src同一目录下。

 

/*
 date:2013/4/18
 Author:zhangrong
 this programe  has a frame that show all files in this computer
 * */
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


import java.io.*;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GetFileClass {

 /**
  * author:zhangrong
  * date:2013/4/21
  * target:简单的一界面实现c,e,f,g盘的文件读取
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub

EventQueue.invokeLater(new Runnable()
{
 public void run()
 {
  new FileFrame();

  
  
 }

});
 }
}
//show the frame
class FileFrame extends JFrame
{
 JList list;
 DefaultListModel listModel;
 JTextArea text;
 JButton back;
 JLabel status;
 
 JRadioButton  C;
 JRadioButton  E;
 JRadioButton  F;
 JRadioButton  D;
 ImageIcon image=new ImageIcon("back02.png");
 JButton backButton;
 
 int isFile;
 JButton deepButton;
 Stack<String>  strSta=null;
 SwingWorker<String[],Void>   FileSwingWorker;
 SwingWorker<StringBuilder,String>  TextSwingWorker;
 static final int DEFAULT_WIDTH=550;
 static final int DEFAULT_HIGHT=450;
 
 FileFrame()
 {
  super("文件便捷框");
  strSta=new Stack<String>();
  strSta.push("c:\\");
  isFile=0;
  setSize(DEFAULT_WIDTH,DEFAULT_HIGHT);
  
  JPanel jpRadioButtons = new JPanel();
   jpRadioButtons.setLayout(new GridLayout(1, 6));
           
   backButton=new JButton(image);
   //back按钮
   backButton.addActionListener(new ActionListener()
   {
   public void actionPerformed(ActionEvent e)
   {
    if(strSta.size()>1)
    {
     strSta.pop();
     File t=new File(strSta.peek());
     status.setText("Current Patn:   "+strSta.peek());
    listModel.clear();
         // FileSwingWorker=new getFile(strSta.peek());
           //  FileSwingWorker.execute();
    
     String []s=t.list();
     for(int i=0;i<s.length;i++)
     {
      String []u=s[i].split("\\\\");
      int len=u.length-1;
      listModel.addElement(u[len]);    
     }
    
    
    }
   
   
   }
   }
   );
   backButton.setOpaque(false);
  

   backButton.setBackground(new Color(2,1,2));
     backButton.setBorder(null);
    
     deepButton=new JButton("下一页");
     deepButton.addActionListener(new ActionListener()
   {
    public void actionPerformed(ActionEvent e)
    {
     if(list.getSelectedValue()!=null)
    {
     String fileSelected=list.getSelectedValue().toString();
    
     String str=strSta.peek()+fileSelected+'\\';
    
     File f=new File(str);
     if(f.isDirectory())
     {
      strSta.push(str);
      FileSwingWorker=new getFile(str);
             FileSwingWorker.execute();
           
     }
     else
     {
     // text.setText("this is a file");
      if(fileSelected.endsWith(".txt"))
      {
      TextSwingWorker=new getText(str);
      TextSwingWorker.execute();
      }
      else
      {
       text.setText("this file doesn't end with .txt, cannot read\n");
      }
     }
     
           
           
           
    }
    
    }
    }
   
    );
    
    
    
    
    
   JPanel pp=new JPanel();
   pp.add(backButton,BorderLayout.CENTER);
   jpRadioButtons.add(pp);
   jpRadioButtons.add(deepButton);
  
  
  
  
  
  
  
  
    jpRadioButtons.add(C = new JRadioButton("C盘"));
    C.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent arg0) {
     listModel.clear();
     strSta.clear();
     strSta.push("c:\\");
     status.setText("Current Path:"+strSta.peek());
      File f=new File(strSta.peek());
      String []s=f.list();
      for(int i=0;i<s.length;i++)
       listModel.addElement(s[i]);
      
      text.setText("you select :"+f.toString()+'\n'+"attribute:");  
     
      text.append("direction"+"\nthe number of files  :"+s.length);
      
      
     validate();
      }});

     //当按下才cdef盘的时候,strsta会清空
     jpRadioButtons.add(D = new JRadioButton("D盘:")); 
     D.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent arg0) {
     listModel.clear();
     strSta.clear();
     strSta.push("d:\\");
     status.setText("Current Path:"+strSta.peek());
      File f=new File(strSta.peek());
      String []s=f.list();
      for(int i=0;i<s.length;i++)
       listModel.addElement(s[i]);
      
      text.setText("you select :"+f.toString()+'\n'+"attribute:");  
      
      text.append("direction"+"\nthe number of files  :"+s.length);
      
     validate();
      
       }});
     jpRadioButtons.add(E = new JRadioButton("E盘:"));
     E.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent arg0) {
      listModel.clear();
      strSta.clear();
      strSta.push("e:\\");
      status.setText("Current Path:"+strSta.peek());
       File f=new File(strSta.peek());
       String []s=f.list();
       for(int i=0;i<s.length;i++)
        listModel.addElement(s[i]);
       text.setText("you select :"+f.toString()+'\n'+"attribute:");  
       
       text.append("direction"+"\nthe number of files  :"+s.length);
       
      validate();
       }});
     jpRadioButtons.add(F = new JRadioButton("F盘:"));
     F.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent arg0) {
      listModel.clear();
      strSta.clear();
      strSta.push("f:\\");
      status.setText("Current Path:"+strSta.peek());
       File f=new File(strSta.peek());
       String []s=f.list();
       for(int i=0;i<s.length;i++)
        listModel.addElement(s[i]);
       text.setText("you select :"+f.toString()+'\n'+"attribute:");  
       
       text.append("direction"+"\nthe number of files  :"+s.length);
       
      validate();
       }});
     ButtonGroup group = new ButtonGroup();
           group.add(C);
           group.add(D);
           group.add(E);
           group.add(F);
           C.setSelected(true);
  
  
  
  
  
  
  add(jpRadioButtons,BorderLayout.NORTH);
  status=new JLabel("          Done   ");
  
  add(status,BorderLayout.SOUTH);
  
  listModel=new DefaultListModel();
  list=new JList(listModel);
  /*list.addListSelectionListener(new ListSelectionListener()
  {

   @Override
   public void valueChanged(ListSelectionEvent arg0) {
    if(list.getValueIsAdjusting())
    {
            String fileSelected=list.getSelectedValue().toString();
            text.setText(""+fileSelected);
          //  listModel.clear();
         FileSwingWorker=new getFile(fileSelected);
            FileSwingWorker.execute();
          
    }
   }
   
  });*/
  
  text=new JTextArea(45,20);
     add(new JScrollPane(list),BorderLayout.CENTER);
  add(new JScrollPane(text),BorderLayout.EAST);
  
  //初始化显示c盘的文件
  File c=new File(strSta.peek());
  String a[]=c.list();
  for(int loop=0;loop<a.length;loop++)
       listModel.addElement(a[loop]);
  text.setText("you select :"+c.toString()+'\n'+"attribute:");
  if(isFile==0)
  {
   text.append("direction"+"\nthe number of files  :"+a.length);
  }
  status.setText("Current Path:   "+strSta.peek());
  setVisible(true);
  this.validate();
  this.setResizable(false);
 }
  
  
  
  
  
  
 
 class  getFile extends SwingWorker<String[],Void>
 {
  String strFile=null;
  String []allFile;
  getFile(String _file) //绝对路径
  {
   strFile=_file;
   
   
  }

  @Override
  protected String[] doInBackground() throws Exception {
   // TODO Auto-generated method stub
   
   File f=new File(strFile);
   allFile=f.list();
   
   text.setText("attribute :  direction"+'\n'+"the number of files :"+allFile.length);
   return allFile;
  }
   public void done()
   {
              try {
    String []t=get();       
            status.setText("      \t YOU select :"+strSta.peek());
    listModel.clear();
               
              for(int i=0;i<t.length;i++)
             {
                String []tt=t[i].split("\\\\");
                int len=tt.length-1;
                listModel.addElement(tt[len]);
               }
             
             
             
             
   } catch (InterruptedException e) { } catch (ExecutionException e) {}
    
   }   
 }
 
 
 //当选取的是文件的时候,获取文件的内容并且显示在text中
 class getText extends SwingWorker<StringBuilder,String>
 {
      String str;
      private StringBuilder textBuilder=new StringBuilder();
      getText(String _str)
      {
       str=_str;
      }
  @Override
  protected StringBuilder doInBackground() throws Exception {
   
   File f=new File(str);
   Scanner in=new Scanner(new FileInputStream(f));
   while(in.hasNext())
   {
    String line=in.nextLine();
    textBuilder.append(line+'\n');
    publish(line);
   }
    return textBuilder;
  }
  
  public void process(List<String> data)
  {
     if(isCancelled())
      return ;
     StringBuilder putStr=new StringBuilder();
     for(String d:data)
     {
      putStr.append(d);
     }
     text.append(putStr.toString());
    
  }
  
  public void done()
  {
   StringBuilder t;
   try {
    t = get();text.setText(t.toString());
   } catch (InterruptedException e) {  } catch (ExecutionException e) {}
   
  }
  
 }
 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值