0

I'm working a transfer file program and my program is working but I'm having a problem because when I select multiple files and put it on a textbox the source directory can't read what is on the textbox

this is my code

Opening file/files

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());
            }
        }
    }
});

Displaying Files on textbox

public void displayFiles(String[] files) {
    for (int i = 0; files != null && i < files.length; i++) {
        txtSource.append(files[i]);
        txtSource.setEditable(false);
    }
}

Copy Files

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: One file is okay but if multiple files are selected and put on the textbox the source directory can't be found

avojak
  • 2,342
  • 2
  • 26
  • 32
jowdislove
  • 27
  • 1
  • 9

1 Answers1

0

In order to be completely helpful, we could really use some more detail (specific exceptions, a complete MCVE, which SWT widgets are used, etc.).

That said, I think you've provided enough to see that there are some issues with your code:

  1. For starters, when you have multiple files selected, you're displaying the same file name (the name of the first one) over and over. Perhaps this is intentional, but worth mentioning:

    String[] selectedFiles = fd.getFileNames();
    File file = new File(firstFile);
    for (int ii = 0; ii < selectedFiles.length; ii++ )
    {
        // You've used a FileDialog, so this should always be true
        if (file.isFile())
        {
            // Will always be the first file
            displayFiles(new String[] { file.toString()});
        }
        else
            displayFiles(file.list());
    }
    
  2. Based on the context, I'm assuming txtSource is a Text widget. With that in mind, if we look at your displayFiles() method, you have the following:

    txtSource.append(files[i]);
    

    When you call displayFiles() repeatedly, you will be tacking on a file name after all the others, effectively building one long String which is the combination of all file names. When you go to copy the files listed, splitting that String back into valid file paths will be tricky.

    My guess is that when you say:

    "the source directory can't be found"

    ...you're just grabbing the content of txtSource. Something like this:

    new File(txtSource.getText());
    

    "...One file is okay..."

    That will certainly work if there's only one file name in the Text object, but if there are multiple names it will result in a non-existent File.

    For example, if you've selected two files:

    • C:\Users\me\FileA
    • C:\Users\me\FileB

    Your txtSource would display C:\Users\me\FileAC:\Users\me\FileB. And the path C:\Users\me\FileAC:\Users\me\FileB most likely does not exist.

    In that case, new File(txtSource.getText()).exists() would return false, and using that File in the constructor for FileInputStream (inside copyFile()) would result in a FileNotFoundException.


In short, just make sure that when you make your call to copyFile() and create the source File object that you're giving the path that you think you are, and not the concatenation of all files selected.

Community
  • 1
  • 1
avojak
  • 2,342
  • 2
  • 26
  • 32
  • thank you for posting this @avojak, and I get your point actually all you had listed on the top is all correct. I did what you said but i can't get what do I change on my codes. – jowdislove Mar 27 '17 at 00:02