【发布时间】:2017-03-24 05:04:15
【问题描述】:
我正在使用传输文件程序,我的程序正在运行,但我遇到了问题,因为当我选择多个文件并将其放在 textbox 上时,源目录无法读取 @987654322 上的内容@
这是我的代码
打开文件
btnSearchFile.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog fd = new FileDialog(shell, SWT.MULTI);
Collection files = new ArrayList();
String firstFile = fd.open();
if (firstFile != null) {
String[] selectedFiles = fd.getFileNames();
File file = new File(firstFile);
for (int ii = 0; ii < selectedFiles.length; ii++ )
{
if (file.isFile())
{
displayFiles(new String[] { file.toString()});
}
else
displayFiles(file.list());
}
}
}
});
在文本框中显示文件
public void displayFiles(String[] files) {
for (int i = 0; files != null && i < files.length; i++) {
txtSource.append(files[i]);
txtSource.setEditable(false);
}
}
复制文件
public static void copyFile(File src, File dest) throws IOException
{
InputStream oInStream = new FileInputStream(src);
OutputStream oOutStream = new FileOutputStream(dest);
// Transfer bytes from in to out
byte[] oBytes = new byte[1024];
int nLength;
BufferedInputStream oBuffInputStream = new BufferedInputStream( oInStream );
while ((nLength = oBuffInputStream.read(oBytes)) > 0)
{
oOutStream.write(oBytes, 0, nLength);
}
oInStream.close();
oOutStream.close();
}
PS:一个文件是可以的,但是如果选择多个文件并放在文本框上,则找不到源目录
【问题讨论】:
-
我无法理解您所说的“源目录无法读取文本框上的内容”是什么意思?您的代码中的
txtSource是什么 - 它是Text小部件还是其他东西? -
抱歉,回复晚了,是的,
txtSource是一个Text小部件。
标签: java swt filedialog