java swt 文件操作 知识点记录

本文详细介绍了如何使用FileDialog进行文件选择及创建、移动、复制、删除和下载等操作,并提供了SWT界面下刷新表格的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

弹出选择多个文件:

FileDialog filedlg = new FileDialog(this.getShell(),SWT.MULTI);

弹出选择单个文件:

FileDialog filedlg = new FileDialog(this.getShell(),SWT.SINGLE);

弹出保存对话框:

FileDialog filedlg = new FileDialog(this.getShell(),SWT.SAVE);

File file = new File("a.txt");

创建文件,移动文件:

// file.createNewFile(); //创建一个相同名字的空文件
// file.renameTo(newFile);//移动文件到指定位置

选择多个文件:

private void selectFile() throws Exception{
        FileDialog filedlg = new FileDialog(this.getShell(),SWT.MULTI);
        //设置文件对话框的标题
        filedlg.setText("Choose File");        
        filedlg.setFilterPath("SystemDrive");
        filedlg.open();
        
        //打开文件对话框,返回选中文件的绝对路径
        String[] selected = filedlg.getFileNames();        
        if(selected.length == 0){
            return;
        }

        
        for (int j = 0; j < selected.length; j++) {
            try {                
                String strName = filedlg.getFilterPath() + "\\" + selected[j];                            
               System.out.println(strName);              
            } catch (IOException e) {
                e.printStackTrace();              
            }
        }
    }    

复制文件方法:

    /**
     *  //文件复制的方法
     * @param from 要复制的文件
     * @param to //复制到的文件
     * @throws Exception
     */
    public void copyFile(File from ,File to) throws Exception{
        
        //构建一个文件输入流对象
        FileInputStream fin=new FileInputStream(from);        
        //构建以个文件输出流对象
        FileOutputStream fout=new FileOutputStream(to);        
        //缓冲输入流
        BufferedInputStream bin=new BufferedInputStream(fin);        
        //缓存输出流
        BufferedOutputStream bout=new BufferedOutputStream(fout);
        
        //定义个字节数组,作为输入流和输出流的中介
        byte [] b=new byte[2048];
        
        //读入的字节长度如果为-1,说明没有内容了
        int len=bin.read(b);
        while(len !=-1){            
            //将字节数组写入输出流中
            bout.write(b,0,len);
            len=bin.read(b);
        }        
        //关闭流,注意顺序
        bout.close();
        fout.close();
        bin.close();
        fin.close();
    }    

删除文件:

public void delFile(String strName) throws Exception{
        
        if(strName.isEmpty() || strName == null){
            log.debug("//fileName is empty!");
            return;
        }
        //删除文件  若存在则删除,不存在则创建
        File currPath = new File(".");
        File currFile = null;
        try {
            String fileName = currPath.getCanonicalPath() + "\\src\\" + strName;
            currFile = new File(fileName);
            log.debug("//fileName:" + fileName);
            
            if(currFile.exists()){
                currFile.delete();    
            }else{
                log.debug("//file is not exist" + fileName);
            }
            
        } catch (IOException e) {
            e.printStackTrace();
            log.debug("//IOException:" + e.getMessage().toString());
        }    
    }    

下载文件:

public void downFile(String strName) throws Exception{
                     
        FileDialog filedlg = new FileDialog(this.getShell(),SWT.SAVE);        
        //设置文件对话框的标题
        filedlg.setText("Save File");        
        filedlg.setFilterPath("SystemDrive");
        filedlg.setFileName(strName);
        String newName = filedlg.open();    
        if (newName == null) {
            return;
        }
                
        File   oldFile = null;
        File   newFile = null;
        File   filePath = new File(".");        
        try {
            
            String oldName = filePath.getCanonicalPath() + "\\src\\file\\" + strName;           
            oldFile = new File(oldName);
            newFile = new File(newName);
            if (!newFile.exists()) {
                copyFile(oldFile,newFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }    

创建文件夹:

private void initDir(){
        File   newFile = null;
        File   filePath = new File(".");
        try {                    
            //创建目录时,src后面不能加\\斜杠
            String newName = filePath.getCanonicalPath() + "\\src";    
            newFile = new File(newName);
            if (!newFile.isDirectory()) {
                newFile.mkdir();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

SWT界面里刷新TABLE的时候,要先dispose:

Table table = new Table(this , SWT.MULTI |SWT.BORDER | SWT.FULL_SELECTION);

    //先释放,要不会报错。
        TableItem tableItems[] = table.getItems();//得到所有的tableItem
        for(int i = 0; i<tableItems.length; i++)
        {
            tableItems[i].dispose();//释放
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值