弹出选择多个文件:
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();//释放
}